Rendering Controls


I mentioned earlier that I would explain the unusual HTML syntax of the controls you put on the page. To look at the syntax again, switch to Web Matrix, and click the HTML tab of the designer. The TextBox control, for example, will look like the following:

<asp:TextBox  runat="server"></ asp:TextBox> 

Switch back to Internet Explorer. (If you closed the browser, just run the page again.) In the browser, show the page s source. (Right-click and choose View Source.) If you look carefully, you ll notice that there is no <asp:Textbox> tag anywhere on the page. In fact, the TextBox control shows up here as:

<input name="TextBox1" type="text"  />

Similarly, <asp:button> has become <input type="submit"> and <asp:Label> has turned into <span >.

If you know HTML, you might know that it offers a pretty paltry list of controls to work with. However, ASP.NET offers a rich set of server controls. Look at the Web Controls tab of the Toolbox in Web Matrix, though, and you ll see all sorts of controls: not only buttons, text boxes and labels, but also a DropDownList, a ListBox, and an ImageButton control, and even controls such as AdRotator and Calendar.

ASP.NET server controls are abstractions of controls. You can add server controls to your pages, set their properties, and write event handlers for them, all as if they were actual controls. When ASP.NET processes a page, it looks for server controls on the page controls with the <asp:> prefix (or some other reserved prefixes that we ll see later in the book) and containing the attribute runat="server". Essentially, ASP.NET processes the server controls and passes other elements on the page through to the browser. (Not quite, but bear with me for the moment.)

When ASP.NET encounters a server control, it processes the control by reading its properties and then constructing rendering the control as HTML. For example, when it sees an <asp:Button> control, it renders an HTML <input type="submit"> tag. Similarly, when it sees an <asp:Label> control, it renders a <span> tag.

For the simple controls you ve seen so far, the value that ASP.NET adds by providing these server controls isn t very obvious. But for other controls, especially the data controls, the ASP.NET control model is very powerful. Instead of thinking of all the fiddly HTML that you would need to display a data grid or a calendar, for example, you ll be able to concentrate on just the logic required for your application.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope

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