Thursday, February 01, 2007

HOME BUILDER

More on the various elements of Microsoft's .NET

JAMES HEIN

This is part two in my coverage of .NET. Note that it is not the aim of this series to make you expert .NET programmers, but rather to highlight some of the elements of .NET programming and how the Microsoft specific elements are used.

When programming with ASP.NET under normal conditions, every HTML element is handed as literal text just like in a regular HTML file. To reference any element you need to convert it to a HTML server control and set the ID field to some unique name.

Just like normal HTML elements, if you have an ID= tag assigned you can then change them under program control.

By default the Page - Load event is triggered when an ASP.NET page is loaded. You can of course add your own code to this event handler.

myHello.visible="true"

myHello.style. etc.

In this case we are making a text tag visible and then changing some of the style attributes. This code would be of course added to the Page - Load event code.

In the past I have had problems with Microsoft controls. After one server security update, for example, I had to rewrite all of my code. With that not-so-subtle warning out of the way, there are two types of server controls you can use in your projects.

One is the basic HTML server control. Open up a project and on one of the pages drop a HTML text box, button and DIV element. Right click on each on and check Run as Server Control.

Now double click on the button to call up the default event code for click. Assuming that you have not changed the names of the textbox or DIV, add the following code before the End Sub line.

DIV1.InnerText = Text1.Value

If you have changed the names of the items then make the appropriate modifications. Run the page by clicking on the small green triangle at the top of the page. Enter some text in the text box and click on the button. The blank DIV element should now display the text you placed in the box.

Congratulations! You have built your first ASP.NET web page and hopefully successfully executed it. Before we go further, a few notes on what you have built. By clicking on the Run as Server Control attribute, this changed each basic HTML client element to run at the server. You can check this by clicking on Source at the button of the page and note that runat="server" has been added to each element.

The line of code you added sets the InnerText property of the DIV line to the current text value held in the HTML text box. You modified the click event subroutine. When you click on the button this triggers the click event and the code in that subroutine, if it exists, is executed. In this case the Inner text property of the DIV object was assigned to Value property of the text object.

We have covered objects, events and properties in the past and methods were mentioned last week. If you are planning to program under .NET, then these four terms are the ones you need to keep in mind each time you write code.

As a refresher, the objects are the things that you use - like the HTML elements. A method is something that occurs. For example, the click method of an object triggered by the click event, and does something either by default or by executing the code you put in it. A property or attribute is something that helps to define or describe the object like its colour, its current value, etc.

If you want an analogy in English, a noun would be the object, a method would be a verb and an adjective would be a property.

You use a HTML server control if you want to access the properties of a control from the server. This type of control is also used if you need to have client-side Java scripts attached.

HTML controls, however, do not have a programming model that matches Windows forms. What this means is that the property name for, say, the contents of a text box under the standard HTML is Value, while under Microsoft Windows Forms it is Text.

A more subtle difference is that a HTML server control maps to a single HTML tag. If you want more complexity you will need to use Microsoft Web Server Controls. We will look at these next week.

Email: jamesh@inet.co.th

Bangkok Post
Wednesday January 31, 2007

No comments: