Adding an ASP Dot Net control

Certify and Increase Opportunity.
Be
Govt. Certified ASP.NET Programmer

Adding an ASP Dot Net control

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.

To add controls to the page

  1. Click the Design tab to switch to Design view.
  2. 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.
  3. In the Toolbox, expand the Standard group.
  4. 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.
  5. Drag a Button control onto the page and position it to the right of the TextBox control.
  6. Drag a Label control onto the page and position it on a separate line below the Button control.
  7. 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.
    Controls in Design View

    Three controls in Design view

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 set properties in both Design view and Source view.

To set control properties

  1. Select the Button control, and then in the Properties window, set Text to Display Name, as shown in the following screen shot.
    Changed Button control text

    Set Button text

  2. 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 <form> 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 <form runat=”server”> and <script runat=”server”> 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.
  3. Put the insertion point after asp:Label in the <asp:Label> 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.
    IntelliSense for Label control

    IntelliSense attributes

  4. Select ForeColor and then type an equal sign and a quotation mark (=”). IntelliSense displays a list of colors.
    Note Note:
    You can display an IntelliSense drop-down list at any time by pressing CTRL+J.
  5. 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.

Programming the Button Control

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.

To add a default button event handler

  1. Switch to Design view.
  2. 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.
    Note Note:
    Double-clicking a control in Design view is just one of several ways you can create event handlers.
  3. Inside the handler, type the following:Label1.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.
    Available Label control members

    IntelliSense in Code view

  4. 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!"
    End Sub
    protected void Button1_Click(object sender, System.EventArgs e)
    {    
        Label1.Text = TextBox1.Text + ", welcome to Visual Web Developer!";
    }
  5. Scroll down to the <asp:Button> element. Note that the <asp:Button> 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.
    Note Note:
    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.

Running the Page

You can now test the server controls on the page.

To run the page

  1. Press CTRL+F5 to run the page in the browser.The page again runs using the ASP.NET Development Server.
  2. 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.
  3. 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 <asp:> 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 <asp:Button> control is rendered as the HTML element <input type=”submit”>.
  4. 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.

To add a Calendar control

  1. In Visual Web Developer, switch to Design view.
  2. 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.
    Calendar control in Design view

    Calendar control in Design view

  3. 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.
    Auto Format dialog box for the Calendar control

    Auto Format dialog box (Calendar control)

  4. From the Select a scheme list, select Simple and then click OK.
  5. Switch to Source view.You can see the <asp:Calendar> element. This element is much longer than the elements for the simple controls you created earlier. It also includes subelements, such as <WeekEndDayStyle>, which represent various formatting settings. The following screen shot shows the Calendar control in Source view.
    Calendar control in Source view

    Calendar control in Source view

Programming the Calendar Control

In this section, you will program the Calendar control to display the currently selected date.

To program the Calendar control

  1. In Design view, double-click the Calendar control.A new event handler is created in Source view.
  2. 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()
    End Sub
    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {    
        Label1.Text = Calendar1.SelectedDate.ToLongDateString();
    }

Running the Page

You can now test the calendar.

To run the page

    1. Press CTRL+F5 to run the page in the browser.
    2. Click a date in the calendar.The date you clicked is displayed in the Label control.
    3. 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 <td> element containing an <a> element.
    4. Close the browser.

Get industry recognized certification – Contact us

Menu