
Adding and Programming Controls
In this part of the walkthrough, you will add a button, text box, and label control to the page and write code to handle the button's Click event. Server controls, which include buttons, labels, text boxes, and other familiar controls, provide typical form-processing capabilities for your ASP.NET Web pages. However, you can program the controls with code that runs on the server, not the client.
To add controls to the page
Click the Design tab to switch to Design view.
Place the insertion pointer after the text that you added previously.
Press ENTER a few times to make some room.
From the Standard tab in the Toolbox, drag three controls onto the page: a TextBox control, a Button control, and a Label control.
Put the insertion pointer in front of the text box and type Enter your name:.
This static HTML text is the caption for the TextBox control. You can mix static HTML and server controls on the same page.
Setting Control Properties
Visual Web Developer offers you various ways to set the properties of controls on the page. In this part of the walkthrough, you will work with properties in both Design view and Source view.
To set control properties
Select the Button control and, in the Properties window, set its Text property to Display Name.
Switch to Source view.
Source view displays the HTML for the page, including the elements that Visual Web Developer has created for the server controls. Controls are declared using HTML-like syntax, except that the tags use the prefix asp: and include the attribute runat="server".
Control properties are declared as attributes. For example, when you set the button's Text property in Step 1, you were actually setting the Text attribute in the control's markup.
Note that all the controls are inside a form element that also has the attribute runat="server". The runat="server" attribute and the asp: prefix for control tags mark the controls so that they are processed by ASP.NET when the page runs.
Programming the Button Control
For this walkthrough, you will write code that reads the name that the user enters in the text box and then displays it in the Label control.
To add a default button event handler
Switch to Design view
Double-click the Button control.
Visual Web Developer opens the WebPageSeparated.aspx.vb or WebPageSeparated.aspx.cs file in a separate window in the editor. The file contains a skeleton Click event handler for the button.
Complete the Click event handler by adding the following highlighted code.
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = Textbox1.Text & ", welcome to Visual Web Developer!"
End Sub
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = TextBox1.Text + ", welcome to Visual Web Developer!";
}
Notice that as you type, IntelliSense helps you with context-sensitive choices. This is identical to the behavior when you are coding in a single-file page.