Compartilhar via


Walkthrough: Creating a Basic Web Forms Page in Visual Studio

This walkthrough provides you with an introduction to the Web development environment in Visual Studio and in Visual Web Developer Express for ASP.NET Web Forms pages. This walkthrough guides you through creating a simple ASP.NET Web Forms page and illustrates the basic techniques of creating a new page, adding controls, and writing code.

Note

This topic applies only to ASP.NET Web Forms pages. It does not apply to pages that you create using ASP.NET MVC (Model View Controller) or ASP.NET Web Pages.

Tasks illustrated in this walkthrough include:

  • Creating a file system Web site project.

  • Familiarizing yourself with Visual Studio.

  • Creating a single-file ASP.NET Web Forms page.

  • Adding controls.

  • Adding event handlers.

  • Running pages by using the Web server that is built into Visual Studio for testing.

Prerequisites

In order to complete this walkthrough, you will need:

  • Visual Studio or Visual Web Developer Express. For download information, see the Visual Studio Development Center Web site.

    Note

    If you are using Visual Studio, this walkthrough assumes that you selected the Web Development collection of settings the first time that you started Visual Studio. For more information, see How to: Select Web Development Environment Settings.

  • The .NET Framework, which is installed when you install Visual Studio or Visual Web Developer Express.

Creating a Web Site Project and a Web Forms Page

In this part of the walkthrough, you will create a Web site project and add a new page to it. You will also add HTML text and run the page in your Web browser.

For this walkthrough, you will create a file system Web site project that does not require that you work with Microsoft Internet Information Services (IIS). Instead, you will create and run your page in the local file system.

A file system Web site project is one that stores pages and other files in a folder that you choose somewhere on your computer. Other Web project options include the following:

  • A local IIS Web site project, which stores your files in a subfolder of the local IIS root (typically, \inetpub\wwwroot\).

  • An FTP site project, which stores files on a remote server that you gain access to across the Internet by using File Transfer Protocol (FTP).

  • A remote IIS Web site project, which stores files on a remote server that you can access across a local network.

  • A Web application project, which is similar to a file system Web site project except you compile it before deployment and deploy it as a dynamic link library (.dll) file. For more information about the difference between Web site projects and Web application projects, see Web Application Projects versus Web Site Projects in Visual Studio.

To create a file system Web site

  1. Open Visual Studio or Microsoft Visual Web Developer Express.

  2. On the File menu, click New Web Site.

    The New Web Site dialog box appears, as shown in the following illustration:

    New Web Site dialog box

  3. Under Installed Templates, click Visual Basic or C# and then select ASP.NET Web Site.

    When you create a Web site project, you specify a template. Each template creates a Web project that contains different files and folders. In this walkthrough, you are creating a Web site based on the ASP.NET Web Site template, which creates files and folders that are typically used in ASP.NET Web sites.

  4. In the Web Location box, select File System, and then enter the name of the folder where you want to keep the pages of your Web site.

    For example, type the folder name C:\BasicWebSite.

  5. Click OK.

    Visual Studio creates a Web project that includes prebuilt functionality for layout (a master page, the Default.aspx and About.aspx content pages, and a cascading style sheet), for Ajax (client script files), and for authentication (ASP.NET membership). When a new page is created, by default Visual Studio displays the page in Source view, where you can see the page's HTML elements. The following illustration shows what you would see in Source view if you created a new Web page named FirstWebPage.aspx.

    Default page in Source view

A Tour of the Visual Studio Web Development Environment

Before you proceed with working on the page, it is useful to familiarize yourself with the Visual Studio development environment. The following illustration shows you the windows and tools that are available in Visual Studio and Microsoft Visual Web Developer Express.

Note

This diagram shows default windows and window locations. The View menu allows you to display additional windows, and to rearrange and resize windows to suit your preferences. If changes have already been made to the window arrangement, what you see will not match the illustration.

The Visual Studio environment

Schematic of IDE windows and tools

To familiarize yourself with the Web designer

  • Examine the preceding illustration and match the text to the following list, which describes the most commonly used windows and tools. (Not all windows and tools that you see are listed here, only those marked in the preceding illustration.)

    • Toolbars. Provide commands for formatting text, finding text, and so on. Some toolbars are available only when you are working in Design view.

    • Solution Explorer window. Displays the files and folders in your Web site.

    • Document window. Displays the documents you are working on in tabbed windows. You can switch between documents by clicking tabs.

    • Properties window. Allows you to change settings for the page, HTML elements, controls, and other objects.

    • View tabs. Present you with different views of the same document. Design view is a near-WYSIWYG editing surface. Source view is the HTML editor for the page. Split view displays both the Design view and the Source view for the document. You will work with the Design and Source views later in this walkthrough. If you prefer to open Web pages in Design view, on the Tools menu, click Options, select the HTML Designer node, and change the Start Pages In option.

    • ToolBox. Provides controls and HTML elements that you can drag onto your page. Toolbox elements are grouped by common function.

    • Server Explorer/Database Explorer. Displays database connections. If Server Explorer is not visible, on the View menu, click Server Explorer or Database Explorer.

      Note

      The Server Explorer window is named Database Explorer in Microsoft Visual Web Developer Express. To see all available menu items in Microsoft Visual Web Developer Express, on the Tools menu, click Settings, and then click Expert Settings.

Creating a New ASP.NET Web Forms Page

When you create a new Web site using the ASP.NET Web Site project template, Visual Studio adds an ASP.NET Web Forms page named Default.aspx. You can use the Default.aspx page as the home page for your Web site. However, for this walkthrough, you will create and work with a new page.

To add a page to the Web site

  1. Close the Default.aspx page. To do this, right-click the tab that displays the file name and then click Close.

  2. In Solution Explorer, right-click the Web site (for example, C:\BasicWebSite), and then click Add New Item.

    The Add New Item dialog box is displayed. The following illustration shows an example of the Add New Item dialog box.

    Add New Item dialog box

  3. In the template list, select Web Form.

  4. In the Name box, type FirstWebPage.

    When you created the Web site project, you specified a default language based on the project template that you selected. However, each time you create a new page or component for your Web site, you can select the programming language for that page or component. You can use different programming languages in the same Web site.

  5. Clear the Place code in separate file check box.

    In this walkthrough, you are creating a single-file Web Forms page with the code and HTML in the same page. The code for ASP.NET Web Forms pages can be located either in the page or in a separate class file.

  6. Click Add.

    Visual Studio creates the new page and opens it.

Adding HTML to the Web Forms Page

In this part of the walkthrough, you will add some static text to the page.

To add text to the page

  1. At the bottom of the document window, click the Design tab to switch to Design view.

    Design view displays the page that you are working on in a WYSIWYG-like way. At this point, you do not have any text or controls on the page, so the page is blank except for a dashed line that outlines a rectangle. This rectangle represents a div element on the page.

  2. Click inside the rectangle that is outlined by a dashed line.

  3. Type Welcome to Visual Web Developer and press ENTER twice.

    The following illustration shows the text you typed in Design view.

    Welcome text in Design view

  4. Switch to Source view.

    You can see the HTML that you created by typing in Design view, as shown in the following illustration.

    Welcome text in Source view

Running the Page

Before you proceed with adding controls to the page, you can try running it. To run a page, you need a Web server. In a production Web site, you use IIS as your Web server. However, to test a page, you can use the Visual Studio Development Server, which runs locally and does not require IIS. For file system Web site projects, the default Web server in Visual Studio and Microsoft Visual Web Developer Express is the Visual Studio Development Server.

To run the page

  1. In Solution Explorer, right-click FirstWebPage.aspx and select Set as Start Page.

  2. Press CTRL+F5 to run the page.

    Visual Studio starts the Visual Studio Development Server. An icon appears in the Windows taskbar to indicate that the Visual Studio Development Server is running, as shown in the following illustration.

    Development Web Server icon

    The page is displayed in the browser. Although the page you created has a file-name extension of .aspx, it currently runs like any HTML page.

    To display a page in the browser you can also right-click the page in Solution Explorer and select View in Browser.

  3. Close the browser.

Adding and Programming Controls

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 Forms pages. However, you can program the controls with code that runs on the server, not the client.

You will add a Button control, a TextBox control, and a Label control to the page and write code to handle the Click event for the Button control.

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 drop 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 drop it to the right of the TextBox control.

  6. Drag a Label control onto the page and drop 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 illustration shows how the three controls appear in Design view.

    Three controls in Design view

Setting Control Properties

Visual Studio 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. The text you entered appears on the button in the designer, as shown in the following illustration.

    Note

    If the Properties window is not displayed, press F4 to display it.

    Set Button text

  2. Switch to Source view.

    Source view displays the HTML for the page, including the elements that Visual Studio 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 illustration shows the IntelliSense drop-down list for the Label control.

    IntelliSense attributes

  4. Select ForeColor and then type an equal sign and a quotation mark (=").

    IntelliSense displays a list of colors.

    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 Studio switches to Source view and creates a skeleton event handler for the Button control's default event, the Click event.

    Note

    Double-clicking a control in Design view is just one of several ways you can create event handlers.

  3. Inside the handler, type Label1 followed by a period (.).

    When you type the period after Label, Visual Studio displays a list of available members for the Label control, as shown in the following illustration.

    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 Studio!"
    End Sub
    
    protected void Button1_Click(object sender, System.EventArgs e)
    {    
        Label1.Text = TextBox1.Text + ", welcome to Visual Studio!";
    }
    
  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 the previous step.

    Event handler methods can have any name; the name you see is the default name created by Visual Studio. The important point is that the name used for the onclick attribute must match the name of a method in the page.

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 Visual Studio 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 <input type="submit"> element.

  4. Close the browser.

Working with Additional Controls

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 System.Web.UI.WebControls.Calendar control to the page and format it.

To add a Calendar control

  1. In Visual Studio, switch to Design view.

  2. From the Standard section of the Toolbox, drag a Calendar control onto the page and drop 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 illustration shows the Calendar control as rendered 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 illustration shows the 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 illustration shows the Calendar control in Source view. (The exact markup that you see in Source view might differ slightly from the illustration.)

    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.

Next Steps

This walkthrough has illustrated the basic features of the Visual Studio page designer. Now that you understand how to create and edit a Web Forms page in Visual Studio, you might want to explore other features. For example, you might want to do the following:

See Also

Tasks

Walkthrough: Creating a Local IIS Web Site in Visual Studio

Walkthrough: Editing Web Sites with FTP in Visual Studio

Concepts

ASP.NET Overview

Other Resources

Visual Studio Web Development Content Map