Tuesday, August 10, 2010

Web server Controls and Adding Web Server controls

Web server Controls


ASP.NET provides programmers a set of Web server controls for creating Web pages that provide more functionality and a more consistent programming model than that of HTML server controls.

Web server controls are able to provide more functionality because they are not tied to a single HTML tag element. Instead, they typically render many HTML tags and may also include client-side JavaScript code. Web server controls also have the ability to detect the Web browser’s capabilities and render the appropriate HTML based on those capabilities.

A Web server control can be added to an .aspx page using the Visual Studio Design view or Source view, or dynamically using code.

Eg: Adding Web Server Controls Using Design View

1. Open a Web page in Visual Studio.

2. Click the Design tab at the bottom of the Web page.

3. Open the Toolbox and click the Standard tab.

4. Drag a Web server control from the Toolbox and drop it on the Web page.

Eg: Adding Web Server Controls Using Source View

1. Open a Web page in Visual Studio.

2. Click the Source tab at the bottom of the Web page.

3. In Source view of the Web page, type the Web server control element and its attributes..

4. Alternatively, you can drag a control from the Toolbox directly into the page source..

Eg: Adding Web Server Controls Dynamically Using Code

1. Open the Web page’s code-behind page in Visual Studio.

2. Create a handler for the Page_PreInit event.

3. In the Page_PreInit method, add code to create a new instance of a TextBox Web server control.

4. After creating the instance, add code to set the control’s ID property.

5. Finally, add the control to the Controls collection of form1. This will make sure the control is output as part of the form.

protected void Page_PreInit(object sender, EventArgs e)
{

TextBox textBoxUserName = new TextBox();
TextBoxUserName.ID = "TextBoxUserName";
form1.Controls.Add(textBoxUserName);


}

No comments: