A Sample Page


We expect that you have some experience developing ASP.NET pages, but for completeness, we will begin with a very simple page that shows the typical contents of an .aspx file and the HTML that it generates. Listing 2-1 presents the code needed to create this page.

Listing 2-1 A very simple .aspx file
 <%@Pagelanguage="c#" %> <html> <head> <title>SamplePage</title> <scriptrunat="server"> publicvoidbutton1_OnClick(objectsender,EventArgse){ label1.Text= "Hello " +textBox1.Text+ "!"; } </script> </head> <body> <formrunat="server"> Enteryourname: <asp:TextBoxrunat="server" id="textBox1"/> <asp:Buttonrunat="server" id="button1" Text="OK" OnClick="button1_OnClick"/> <hr> <asp:Labelrunat="server" id="label1"/> </form> </body> </html> 

When a user requests this page, fills in some text, and then submits the page to the server, the page executes on the server and produces HTML content similar to that shown in Listing 2-2.

Listing 2-2 HTML content generated by the sample page
 <html> <head> <title>SamplePage</title> </head> <body> <formname="_ctl0" method="post" action="MyPage.aspx" id="_ctl0"> <inputtype="hidden" name="__VIEWSTATE" value="dDwtMTM3NjQ2NjY2NTt0PDt sPGk8MT47PjtsPHQ8O2w8aTw1Pjs+O2w8dDxwPHA8bDxUZXh0Oz47bDxIZWxsbyBXb3JsZ Ds+Pjs+Ozs+Oz4+Oz4+Oz4=" /> 
 Enteryourname: <inputname="textBox1" type="text" value="World" id="textBox1" /> <inputtype="submit" name="button1" value="OK" id="button1" /> <hr> <spanid="label1">HelloWorld!</span> </form> </body> </html> 

In general, an ASP.NET page consists of static text and markup text (such as HTML elements), with server controls interspersed. Server controls are represented by tags with a runat="server" attribute. On the other hand, literal text and tags without this special attribute are treated as static text. The sample page shown in Listing 2-1 has a few basic server controls. For example, it contains the <asp:TextBox> tag, which represents the standard ASP.NET TextBox Web control. On the server, the TextBox control renders itself as the <input type="text"> HTML element, as shown in the generated HTML content in Listing 2-2.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net