Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 4. | 2

Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 4.

The HtmlForm control creates a server-side <form> tag. Most HTML controls, Web controls, etc., must be placed inside an HtmlForm control.

<form runat="server">
<!-- ASP.NET controls in here -->
</form>

The HtmlImage control creates a server-side <img> tag. The following code shows how we might place an HtmlImage control on a page, along with an HtmlButton:

<img id="myimage" src="arrow.gif" runat="server" />
<button id="myButton" runat="server" OnServerClick="Click">Click 
Here</button>

The user could change this image dynamically by pressing the button if we add code as follows:

<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
  myimage.Src = "welcome.gif"
End Sub
</script>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
  myimage.Src = "welcome.gif";
}
</script>

What will happen if these controls are placed on a page along with the script block? First of all, the image arrow.gif will appear. When the HtmlButton control is clicked, it changes to welcome.gif. Behind the scenes, the ServerClick event is raised when the button is clicked, thus the Click() subroutine is called, and the Src property of the HtmlImage control is changed from arrow.gif to welcome.gif.

The HtmlGenericControl creates a server-side control for HTML tags that do not have an HTML control associated with them. Perfect examples of this are the <span> and <div> tags. The following example illustrates how you can modify text within a <span> tag to change the content from I like ASP.NET to Why would anyone need PHP? dynamically.

<span id="myGenericControl" runat="server">I like ASP.NET</span>
<br />
<button id="myButton" runat="server" OnServerClick="Click">Click 
Here</button>

We simply add the following code to respond to the ServerClick event and change the text:

<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
  myGenericControl.InnerText = "Why would anyone need PHP?"
End Sub
</script>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
  myGenericControl.InnerText = "Why would anyone need PHP?";
}
</script>

The HtmlSelect control creates a server-side version of the <select> tag for creating drop-down lists or list boxes. The following code creates a drop-down menu:

Select your favorite movie:<br />
<select id="selectMovie" runat="server">
<option>Star Wars</option>
<option>Spider Man</option>
<option>The Godfather</option>
<option>Lord of the Rings</option>
</select>

The following code creates a multiple-selection list box:

Which of these movies do you like?<br />
<select id="selectMovie" runat="server" multiple="true" size="4">
<option>Star Wars</option>
<option>Spider Man</option>
<option>The Godfather</option>
<option>Lord of the Rings</option>
</select>

You’ll notice the <option> tag within the main <select> tag; this is used to denote each item to appear in the list box or drop-down menu.

The HtmlTable, HtmlTableRow, and HtmlTableCell controls create server-side versions of the <table>, <tr>, <td>, and <th> tags. The following code creates a server-side table:

<table id="myTable" border="1" cellspacing="0" cellpadding="0" 
runat="server"> 
<tr runat="server" id="row1">
<td runat="server" id="cell1">Table Data 1</td>
<td runat="server" id="cell2">Table Data 2</td>
</tr>
<tr runat="server" id="row2">
<td runat="server" id="cell3">Table Data 3</td>
<td runat="server" id="cell4">Table Data 4</td>
</tr>
</table>
<button id="myButton" OnServerClick="Click" runat="server">Click 
Here</button>

You could add the following code to respond to the Click event raised by the HtmlButton control and change the content of the first cell to read "Hello World."

<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
  cell1.InnerText = "Hello World"
End Sub
</script>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
  cell1.InnerText = "Hello World";
}
</script>