Microsoft KB Archive/302342

= HOW TO: Add ASP.NET Server Controls to a Web Form and Change Their Properties =

Article ID: 302342

Article Last Modified on 6/16/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft ASP.NET 1.1
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition

-



This article was previously published under Q302342



IN THIS TASK
SUMMARY
 * Requirements
 * Create the ASP.NET Web Application Project
 * Add Server Controls to the ASP.NET Page
 * Change the Properties of the Controls

REFERENCES



SUMMARY
This step-by-step article describes how to add ASP.NET server controls, such as a Button control or a Label control, to an existing Web Form.

ASP.NET Web Forms extend beyond the functionality of Hypertext Markup Language (HTML) and script ASP pages. ASP.NET Web Forms add a design model and rich programmatic interaction that is similar to Visual Basic forms. The ASP.NET server controls provide additional properties and events that are not available to HTML controls.

For more information about when to use ASP.NET server controls, refer to REFERENCES section.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET
 * Available Web server that is running Microsoft Internet Information Server (IIS) with Microsoft .NET Framework installed
 * Microsoft Windows 2000 Advanced Server, Microsoft Windows 2000 Server, or Microsoft Windows NT 4.0 Server

This article assumes that you are familiar with Visual Studio .NET.

back to the top

Create the ASP.NET Web Application Project

 * 1) Start Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click any language under Project Types, and then click ASP.NET Web Application under Templates.
 * 4) In the Location text box, type http:// /AddServerControl where   is either localhost if you are running a Web server on this computer or the name of another server on your network.
 * 5) Click OK. Notice that WebForm1.aspx opens in Design view by default.

back to the top

Add Server Controls to the ASP.NET Page
 Make sure that the toolbox is visible. If not, click Toolbox on the View menu, or press the CTRL+ALT+X keyboard shortcut. In the toolbox, click Web Forms. Drag a Button control onto the Web Form in Design view. You can place the control anywhere on the grid. Drag a Label control from the toolbox onto the Web Form near the Button control that you added in the previous step.  Switch to HTML view to review the default code:    Label </asp:Label> Notice the following: <ul> The markup tags for the Button and the Label controls are based on Extensible Markup Language (XML) syntax. Unlike HTML, you must close each tag and enclose the attributes in quotation marks.</li> The markup tags for the Button and the Label controls use the asp: namespace prefix. This indicates that the control is an ASP.NET server control.</li> The Visual Studio integrated development environment (IDE) generates a style attribute. This attribute reflects where you positioned each element on the Web Form.</li></ul> </li> Switch to Design view. Drag a ListBox control onto the Web Form under the Button and the Label controls.</li></ol>

back to the top

Change the Properties of the Controls
<ol> Click the Button control, and then press F4 to view the properties of the button. (Alternately, you can click the Button control and then click Properties Window on the View menu to view the properties of the button.)</li> In the Properties window, change the Text property of the Button control to OK, and then change the ID property of the Button control to cmdOK .</li> Click the Label control, and then press F4 to view its properties.</li> In the Properties window, change the Text property of the Label control to Click this button .</li> Click the ListBox control, and then press F4 to view its properties.</li> In the Properties window, click the Items property, and then click the ellipsis (...) button.</li> In the ListItem Collection Editor, follow these steps to add two ListItem members: <ol style="list-style-type: lower-alpha;"> Click Add to add the first ListItem member.</li> Change the Text property to Choice A, and then change the Value property to A .</li> Change the Selected property to True, and then click OK.</li> Click Add to add the second ListItem member.</li> Change the Text property to Choice B, and then change the Value property to B .</li> <li>Click OK.</li></ol> </li> <li>Save your application.</li></ol>

back to the top

<div class="references_section">