ASP.NET Web Forms Web Forms Server Controls Programming Web Forms Server Controls
Adding Web server controls to a Web Forms page is like adding any HTML element, except that you declare them with an XML tag that references the asp namespace. You can add a Web server control by declaring it directly in the .aspx file.
NOTE: You can also add controls programmatically at run time. For details, see Adding Controls to a Web Forms Page Programmatically.
Type the element representing the control into the .aspx file. The exact syntax you use depends on the control you are adding, but in general the following applies:
- Controls must include the attribute runat=server.
- Set the control’s id attribute unless the control is part of a complex control and will be repeated ( as in Repeater, DataList, and DataGrid controls ) .
- Web server controls are declared with an XML tag that references the asp namespace.
- Control declarations must be properly closed. You can specify an explicit closing tag, or if the control has no child elements, by self-closing the tag. The only exceptions are HTML input controls that cannot have child elements, such as the input controls ( for example, HtmlInputText, HtmlImageand HtmlButton ) .
- Control properties are declared as attributes.
The following examples show typical declarations for Web server controls:
<!-- Textbox ASP.NET control -->
<asp:textbox id=TextBox1 runat="Server" Text=""></asp:textbox>
<!-- Same, but with self-closing element -->
<asp:textbox id=Textbox1 runat="Server" >
<!-- Web DropDownList control, which contains subelements -->
<asp:DropDownList id=DropDown1 runat="server">
<asp:ListItem Value="0">0</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
<asp:Repeater id=Repeater2 runat="server">
<headertemplate">
Company data:
</headertemplate>
<itemtemplate>
<asp:Label runat="server"
Font-Name="verdana" Font-Size="10pt"
Text='<%# ( ( PositionData ) Container.DataItem ) .Name %>’ />
( <asp:Label runat=server
Font-Name="verdana" Font-Size="10pt"
Text='<%# ( ( PositionData ) Container.DataItem ) .Ticker %>’/>
)
</itemtemplate>
<separatortemplate">
,
</separatortemplate>
</asp:Repeater>
For information about the declarative syntax for a specific control, see ASP.NET Syntax for Web Controls.
Adding Controls to a Web Forms Page Programmatically Adding HTML Server Controls to a Web Forms Page Setting HTML Server Control Properties Programmatically Setting Web Forms Server Control Properties Web Forms Validation