There are 3 types of controls that can be added to the asp.net page:
HTML server Controls--these controls simulate HTML elements, and HTML developers are familiar with them.
Web server Control-This is a new set of controls with the same functionality as HTML controls, but their properties and other elements have a common naming pattern that is easy to develop and can be consistent with similar Windows Forms controls. There are several types of Web server controls, including standard controls such as buttons, validation controls that validate user input, user-managed login controls, and some more complex controls that handle data sources.
customizing controls and user controls-controls defined by developers.
All Web server controls are used in the following way from XML elements:
| The code is as follows |
Copy Code |
|
<asp:x runat= "Server" attribute= "value" >Contents</asp:X> |
where x is the name of the ASP.net server control, attribute= "value" is one or more property specifications, contents the contents of the specified control.
Note that the syntax of the controls is xml-based (they can also be embedded in non-XML code, such as HTML). Omitting a closed tag, an empty element/>, or overlapping controls can result in an error.
The runat= "Server" property on the Web server control, left this property, will result in a Web form that cannot be run.
For the first example, two Web Forms controls, labels, and buttons are added:
| The code is as follows |
Copy Code |
|
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:label runat= "Server" id= "resultlable"/><br>
<asp:button runat= "Server" id= "Triggerbutton" text= "click Me"/>
</div>
</form>
</body>
|
Look at the design screen (click "Design" in the lower-left corner of the code window), where the controls have been added and named with their ID attributes. As with Windows Forms, you can access all properties, events, and so on through the Properties window.
All server controls that you add are automatically part of the object model that is built for the form in this post code.
To get this application to do some work, you should add an event handler that clicks the button, you can enter a method name for the button in the Propertier window, or double-click the button to get the default event handler. If you double-click a button, you can automatically add an event-handling method:
| The code is as follows |
Copy Code |
|
protected void Triggerbutton_click (object sender, EventArgs e)
{
} |
By adding some code to the default.aspx, you can link the event handler to the button:
| The code is as follows |
Copy Code |
|
<div>
<asp:label runat= "Server" id= "resultlable"/><br>
<asp:button runat= "Server" id= "Triggerbutton" text= "click Me" onclick= "Triggerbutton_click"/>
</div> |
Where the OnClick property tells the ASP.net runtime to wrap the button's click event into the Triggerbutton_click method when the form's code model is generated.
To modify the code for Triggerbutton_click ():
| The code is as follows |
Copy Code |
|
protected void Triggerbutton_click (object sender, EventArgs e)
{
resultlable.text= "button clicked!"
} |
The following is ready to run it, saving all content. Point your Web browser to the address of your Web site. If you are using IIS, the URL you point to is entered in the browser. If you are using a built-in Web server, you need to start the run, the shortcut key is CTRL+F5, start the server, open a browser, and point to the specified URL.
After running, generally use IE browser, so right click on the page, "View Source code". Can be found:
| The code is as follows |
Copy Code |
|
<form Name= "Form1" method= "post" action= "default.aspx" Id= "Form1"
<div>
<input type= "hidden" Name= "__viewstate" id= "__viewstate" value= " Wepdwuklte2mjy5mty1nq9kfgicaw9kfgicaq8pfgiebfrlehqfd0j1dhrvbibjbglja2vkiwrkzptxxbnqhninco99sernwrmitg2q "/>
</div>
<div>
<input type= "hidden" name= __eventvalidation "id=" __ Eventvalidation "value="/WEWAGKWM6IRAGLHPP+YC30QWQA4YECRIE5FA3FFIB7AGLXL/>
</div>
<div
<span id= "resultlable" >button clicked!</span><br>
<input type= "Submit" name= "Triggerbutton" value= "click Me" id= "Triggerbutton"/>
</div>
</form> |
The
Web server control generates the html,<span> and <input> representatives <asp:Lable> and <asp:Button> respectively. There is also a <input type= "hidden" named __viewstate, which encapsulates the state of the form mentioned previously. Use this information on the form transfer server to recreate the UI while tracking changes. Note that the <form> element is configured to transfer data back to Default.aspx (specified in the action) via an HTTP POST operation (specified in method), and it is also given a name Form1.