asp.net|web| Server | control
In addition to code and markup, ASP.net 2.0 pages can also contain server controls, which are programmable server-side objects that typically appear as UI elements (such as text boxes or images) on a page. The server control participates in the execution of the page and generates its own markup rendering for the client. The advantage of server controls is that it allows developers to derive complex rendering and operational behavior from simple, modular components, greatly reducing the amount of code needed to generate dynamic Web pages, and the other is that customizing their presentation and behavior is simple. The properties exposed by a server control can be set in a declarative (in markup) or programmatically (in code). The server control (and the page control itself) also exposes events that developers can handle in the course of a page execution, or in response to a client action (postback) that sends a page back to the server, the specific actions that are required to perform. Server controls also simplify the issue of reserving State information, which automatically preserves values between multiple successful "postback" operations.
A server control is declared in an. aspx file using a custom tag or an intrinsic HTML tag, and it contains the runat= "Server" property value. The intrinsic HTML markup is handled by a control in the System.Web.UI.HtmlControls namespace. Tags that are not explicitly mapped to a control are specified as System.Web.UI.HtmlControls.HtmlGenericControl types.
The following example uses four server controls: <form runat=server>, <asp:textbox runat=server>, <asp:dropdownlist runat=server>, and <asp:button runat= server>. These server controls automatically generate HTML content at run time.
<form action= "intro4_vb.aspx" method= "POST" runat=server>
Category: <asp:dropdownlist id= "Category" runat=server>
<asp:listitem >psychology </asp:listitem>
<asp:listitem >business </asp:listitem>
<asp:listitem >popular_comp </asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text= "Lookup" runat= "Server"/>
</form>
Note that these server controls automatically retain the values entered by clients that travel to and from the server. These control states are not stored on the server (they are stored in the <input type= "hidden" form field between the requests). It does not require client script.
In addition to supporting standard HTML input controls, ASP. NET also allows developers to use rich custom controls in their pages. For example, the following example shows how to use the <asp:adrotator> control to dynamically display a scrolling advertisement on a page.
<form action= "intro5_vb.aspx" method= "POST" runat= "Server"
<asp:adrotator advertisementfile= "Ads.xml" bordercolor= "Black" borderwidth=1 "server"/>
Category: <asp:dropdownlist id= "Category" runat=server>
<asp:listitem >psychology </asp:listitem>
<asp:listitem >business </asp:listitem>
<asp:listitem >popular_comp </asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text= "Lookup" runat= "Server"/>
</form>
Handling Server Control Events
Each asp.net server control can expose an object model that contains properties, methods, and events. Asp. NET developers can use this object model to clearly modify the page and interoperate with the page.
The following example shows how the asp.net page developer handles the onclick event of the <asp:button runat=server> control to change the Text property of the <asp:label runat=server> control.
<link rel= "stylesheet" href= "Intro.css"
<script language= "VB" runat=server>
Sub SubmitBtn_Click (Sender as Object, E as EventArgs)
Message.Text = "Hi" & Httputility.htmlencode (Name.text) & ", you selected:" & Category.SelectedItem.Text
End Sub
</script>
<body>
<center>
<form action= "intro6_vb.aspx" method= "POST" runat= "Server"
<asp:adrotator advertisementfile= "Ads.xml" bordercolor= "Black" borderwidth=1 "server"/>
Category: <asp:dropdownlist id= "Category" runat=server>
<asp:listitem >psychology </asp:listitem>
<asp:listitem >business </asp:listitem>
<asp:listitem >popular_comp </asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text= "Lookup" runat= "Server"/>
<p>
<asp:label id= "message" runat= "Server"/>
</form>
</center>
</body>
This simple example is similar to the "Intro3" example feature shown earlier. Note that in this new server-based control example, the code becomes very clear and simple. We will also see, ASP. NET page framework component also exposes a large number of page-level events, in which you can write code that executes at a specific time. These events include Page_Load and Page_render.
Using server Controls
Asp. NET server controls are defined in a page using declarative markup that contains the runat= "server" attribute. The following example declares three <asp:label runat= "Servers" server controls and defines the text and style properties for each control.
<body>
<font face= "Verdana" >declaring Server Controls </font>
This sample demonstrates you to declare the <asp:label> server control and
Manipulate its properties within a page.
<p>
<asp:label id= "Message1" font-size= "font-bold=" true "forecolor=" Red "runat=server>this is message one </asp:label>
<br>
<asp:label id= "Message2" font-size= "font-italic=" true "forecolor=" Blue "Runat=server>this is message two</asp: label>
<br>
<asp:label id= "Message3" font-size= "font-underline=" true "forecolor=" green "runat=server>this is message three</ asp:label>
</body>
Manipulating server Controls
You can programmatically identify the server control by providing the id attribute of the asp.net server control, or you can programmatically manipulate the object model of the server control at run time by using this ID pointer. For example, the following example demonstrates how a page developer can programmatically set the Text property of the <asp:label runat= "Server" control in the Page_Load event.
<script language= "VB" runat= "Server"
Sub Page_Load (Sender as Object, E as EventArgs)
Message.Text = "You accessed the This page at:" & DateTime.Now
End Sub
</script>
<body>
<font face= "Verdana" >manipulating Server Controls </font>
This sample demonstrates you to manipulate the <asp:label> server control within
The Page_Load event to output the current time.
<p>
<asp:label id= "message" font-size= "a" font-bold= "true" runat=server/>
</body>
Handling events for controls
Asp. NET server controls can also expose and raise server events for page developers to handle. The page developer can do this by declaring an event to each control (in which case the event's property name indicates the name of the event, and the value of the property indicates the name of the method being invoked). For example, the following code example demonstrates how to write an onclick event to a button control.
<script language= "VB" runat= "Server"
Sub EnterBtn_Click (Sender as Object, E as EventArgs)
Message.Text = "Hi" & Name.text & ", Welcome to Asp.net!"
End Sub
</script>
<body>
<font face= "Verdana" >handling control Action Events </font>
<p>
This sample demonstrates how to access a <asp:textbox> server control within the "click" Event of a <ASP:BUTTON&G t;, and use it content to modify the text of a <asp:label>.
<p>
<form action= "Controls3.aspx" runat=server>
<font face= "Verdana" > Please enter your name:
<asp:textbox id= "Name" runat=server/>
<asp:button text= "Enter" runat=server/>
<p>
<asp:label id= "Message" runat=server/>
</font>
</form>
</body>
handling multiple Server events
Event handlers provide a clear way for page developers to construct logic in asp.net pages. For example, the following example shows how to handle four button events on a single page.
<script language= "VB" runat= "Server"
Sub Addbtn_click (Sender as Object, E as EventArgs)
If Not (Availablefonts.selectedindex =-1)
INSTALLEDFONTS.ITEMS.ADD (New ListItem (AvailableFonts.SelectedItem.Value))
AvailableFonts.Items.Remove (AvailableFonts.SelectedItem.Value)
End If
End Sub
Sub Addallbtn_click (Sender as Object, E as EventArgs)
Do as not (AvailableFonts.Items.Count = 0)
INSTALLEDFONTS.ITEMS.ADD (New ListItem (Availablefonts.items) (0). Value))
AvailableFonts.Items.Remove (Availablefonts.items (0). Value)
Loop
End Sub
Sub Removebtn_click (Sender as Object, E as EventArgs)
If Not (Installedfonts.selectedindex =-1)
AVAILABLEFONTS.ITEMS.ADD (New ListItem (InstalledFonts.SelectedItem.Value))
InstalledFonts.Items.Remove (InstalledFonts.SelectedItem.Value)
End If
End Sub
Sub Removeallbtn_click (Sender as Object, E as EventArgs)
Do as not (InstalledFonts.Items.Count = 0)
AVAILABLEFONTS.ITEMS.ADD (New ListItem (Installedfonts.items) (0). Value))
InstalledFonts.Items.Remove (Installedfonts.items (0). Value)
Loop
End Sub
</script>
<body>
<font face= "Verdana" >handling multiple control Action Events </font>
<p>
This sample demonstrates the how to handle multiple control action events raised from
Different <asp:button> controls.
<p>
<form action= "Controls4.aspx" runat=server>
<table>
<tr>
<td>
Available Fonts
</td>
<td>
!--Filler-->
</td>
<td>
Installed Fonts
</td>
</tr>
<tr>
<td>
<asp:listbox id= "availablefonts" width= "100px" runat=server>
<asp:listitem> Roman </asp:listitem>
<asp:listitem> Arial Black </asp:listitem>
<asp:listitem> Garamond </asp:listitem>
<asp:listitem> Somona </asp:listitem>
<asp:listitem> Symbol </asp:listitem>
</asp:listbox>
</td>
<td>
!--Filler-->
</td>
<td>
<asp:listbox id= "installedfonts" width= "100px" runat=server>
<asp:listitem> Times </asp:listitem>
<asp:listitem> Helvetica </asp:listitem>
<asp:listitem> Arial </asp:listitem>
</asp:listbox>
</td>
</tr>
<tr>
<td>
!--Filler-->
</td>
<td>
<asp:button text= "<<" runat=server/>
<asp:button text= "runat=server/>"
<asp:button text= ">" runat=server/>
<asp:button text= ">" runat=server/>
</td>
<td>
!--Filler-->
</td>
</tr>
</table>
</form>
</body>
perform page navigation (first case)
In a real web application, navigation between multiple pages is common. The following example shows how to use the <asp:hyperlink runat=server> control to navigate to another page (with the custom query string parameter passed). This example then shows how to easily get these query string parameters on the target page.
<script language= "VB" runat= "Server"
Sub Page_Load (Sender as Object, E as EventArgs)
Dim Randomgenerator as Random
Randomgenerator = New Random (DateTime.Now.Millisecond)
Dim Randomnum as Integer
Randomnum = Randomgenerator.next (0, 3)
Select Randomnum
Case 0:
Name.text = "Scott"
Case 1:
Name.text = "Fred"
Case 2:
Name.text = "Adam"
End Select
Anchorlink.navigateurl = "Controls_navigationtarget_vb.aspx?name=" & System.Web.HttpUtility.UrlEncode ( Name.text)
End Sub
</script>
<body>
<font face= "Verdana" >performing Page navigation (scenario 1) </font>
<p>
This is sample demonstrates how to generate a HTML Anchor tag that'll cause the client to
Navigate to a new page when he/she clicks it within the browser.
<p>
<p>
<asp:hyperlink id= "Anchorlink" font-size=24 runat=server>
Hi <asp:label id= "Name" runat=server/> please click this link!
</asp:hyperlink>
</body>
perform page navigation (second case)
Not all page navigation is initiated by the client's hyperlink. Asp. NET page developer calls the Response.Redirect (URL) method can also initiate redirection or navigation of the client page. This situation typically occurs when the server side needs to authenticate the input of the client before the actual navigation is performed.
The following example shows how to use the Response.Redirect method to pass parameters to another target page. It also shows how to simply get these parameters on the target page.
<script language= "VB" runat= "Server"
Sub EnterBtn_Click (Sender as Object, E as EventArgs)
If Not (Name.text = "")
Response.Redirect ("Controls_navigationtarget_vb.aspx?name=" & System.Web.HttpUtility.UrlEncode (Name.text))
Else
Message.Text = "hey! Please enter your name in the textbox! "
End If
End Sub
</script>
<body>
<font face= "Verdana" >performing Page navigation (scenario 2) </font>
<p>
This sample demonstrates you to navigate to a new page from within a <asp:button> click event, passing a <asp:tex Tbox> value as a querystring argument (validating a legal textbox value has been).
<p>
<form action= "Controls6.aspx" runat=server>
<font face= "Verdana" >please Enter your name:
<asp:textbox id= "Name" runat=server/>
<asp:button text= "Enter" runat=server/>
<p>
<asp:label id= "message" forecolor= "Red" font-bold= "true" runat=server/>
</font>
</form>
</body>