Your shopping cart is empty!
In this part of the walkthrough, you will add a Button, a TextBox, and a Label control to the page and write code to handle the Click event for the Button control.
You will now add server controls to the page. 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.
Click the Design tab to switch to Design view.
Put the insertion point at the end of the Welcome to Visual Web Developer text and press ENTER five or more times to make some room in the div element box.
In the Toolbox, expand the Standard group.
Drag a TextBox control onto the page and position it in the middle of the div element box that has Welcome to Visual Web Developer in the first line.
Drag a Button control onto the page and position it to the right of the TextBox control.
Drag a Label control onto the page and position it on a separate line below the Button control.
Put the insertion point above the TextBox control, and then 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. The following screen shot shows how the three controls appear in Design view.
Visual Web Developer offers you various ways to set the properties of controls on the page. In this part of the walkthrough, you will set properties in both Design view and Source view.
Select the Button control, and then in the Properties window, set Text to Display Name, as shown in the following screen shot.
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 Text property for the Button control, in step 1, you were actually setting the Text attribute in the control's markup.
Note that all the controls are inside a element, which 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 on the server when the page runs. Code outside of and elements is sent unchanged to the browser, which is why the ASP.NET code must be inside an element whose opening tag contains the runat="server" attribute.
Put the insertion point after asp:Label in the tag, and then press SPACEBAR.
A drop-down list appears that displays the list of properties you can set for a Label control. This feature, referred to as IntelliSense, helps you in Source view with the syntax of server controls, HTML elements, and other items on the page. The following screen shot shows the IntelliSense drop-down list for the Label control.
Select ForeColor and then type an equal sign and a quotation mark (="). IntelliSense displays a list of colors.
You can display an IntelliSense drop-down list at any time by pressing CTRL+J.
Select a color for the Label control's text. Make sure you select a color that is dark enough to read against a white background.
The ForeColor attribute is completed with the color that you have selected, including the closing quotation mark.
For this walkthrough, you will write code that reads the name that the user enters into the text box and then displays the name in the Label control.
Switch to Design view.
Double-click the Button control.
Visual Web Developer switches to Source view and creates a skeleton event handler for the Button control's default event, the Click event.
Double-clicking a control in Design view is just one of several ways you can create event handlers.
Inside the handler, type the following:
When you type the period after Label, Visual Web Developer displays a list of available members for the Label control, as shown in the following screen shot.
Finish the Click event handler for the button so that it reads as shown in the following code example.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = Textbox1.Text & ", welcome to Visual Web Developer!"
protected void Button1_Click(object sender, System.EventArgs e)
Label1.Text = TextBox1.Text + ", welcome to Visual Web Developer!";
Scroll down to the element. Note that the element now has the attribute OnClick="Button1_Click". This attribute binds the button's Click event to the handler method you coded in step 4.
Event handler methods can have any name; the name you see is the default name created by Visual Web Developer. The important point is that the name used for the OnClick attribute must match the name of a method in the page.
If you are using Visual Basic with code separation, Visual Web Developer does not add an explicit OnClick attribute. Instead, the event is bound to the handler method using a Handles keyword on the handler declaration itself.
You can now test the server controls on the page.
Press CTRL+F5 to run the page in the browser.
The page again runs using the ASP.NET Development Server.
Enter a name into the text box and click the button.
The name you entered is displayed in the Label control. Note that when you click the button, the page is posted to the Web server. ASP.NET then recreates the page, runs your code (in this case, the Button control's Click event handler runs), and then sends the new page to the browser. If you watch the status bar in the browser, you can see that the page is making a round trip to the Web server each time you click the button.
In the browser, view the source of the page you are running.
In the page source code, you see only ordinary HTML; you do not see the elements that you were working with in Source view. When the page runs, ASP.NET processes the server controls and renders HTML elements to the page that perform the functions that represent the control. For example, the control is rendered as the HTML element .
Close the browser.
In this part of the walkthrough, you will work with the Calendar control, which displays dates a month at a time. The Calendar control is a more complex control than the button, text box, and label you have been working with and illustrates some further capabilities of server controls.
In this section, you will add a Calendar control to the page and format it.
In Visual Web Developer, switch to Design view.
From the Standard section of the Toolbox, drag a Calendar control onto the page and position it below the div element that contains the other controls:
The calendar's smart tag panel is displayed. The panel displays commands that make it easy for you to perform the most common tasks for the selected control. The following screen shot shows the Calendar control as rendered in Design view.
In the smart tag panel, choose Auto Format.
The Auto Format dialog box is displayed, which allows you to select a formatting scheme for the calendar. The following screen shot shows the Auto Format dialog box for the Calendar control.
From the Select a scheme list, select Simple and then click OK.
You can see the element. This element is much longer than the elements for the simple controls you created earlier. It also includes subelements, such as , which represent various formatting settings. The following screen shot shows the Calendar control in Source view.
In this section, you will program the Calendar control to display the currently selected date.
In Design view, double-click the Calendar control.
A new event handler is created in Source view.
Finish the SelectionChanged event handler with the following highlighted code.
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = Calendar1.SelectedDate.ToLongDateString()
protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
Label1.Text = Calendar1.SelectedDate.ToLongDateString();
You can now test the calendar.
Click a date in the calendar.
The date you clicked is displayed in the Label control.
In the browser, view the source code for the page.
Note that the Calendar control has been rendered to the page as a table, with each day as a