Поделиться через


Walkthrough: Using Shared Code in Web Sites in Visual Web Developer

When you are creating Web site projects, Visual Studio lets you easily create shared code in class files, which can then be used by pages in your application, even without compiling the class files.

In this walkthrough, you will create a simple class and then use it in an ASP.NET Web page.

Tasks illustrated in this walkthrough include the following:

  • Adding a class to a Web site.

  • Having Visual Studio reference the component automatically.

Note

If you have existing assemblies (.dll files), you can add them to the Bin directory of the Web site, and then the assemblies are automatically referenced by the Web site.

Prerequisites

In order to complete this walkthrough, you will need the following:

  • Visual Studio or Visual Web Developer Express.

  • The .NET Framework.

This walkthrough assumes that you have a general understanding of working in Visual Studio. For an introduction, see Walkthrough: Creating a Basic Web Page in Visual Studio.

Creating the Web Site Project

If you have already created a Web site project in Visual Studio (for example, by completing either Walkthrough: Creating a Basic Web Page in Visual Studio or Walkthrough: Creating a Local IIS Web Site in Visual Studio), you can use that project and go to the next section. Otherwise, create a new Web site project by following these steps.

To create a file system Web site project

  1. Open Visual Studio.

  2. On the File menu, click New Web Site. (In Visual Web Developer Express, on the File menu, click New, and then click Web Site.)

    The New Web Site dialog box appears.

  3. Under Installed Templates, select the programming language you want to work with and then select ASP.NET Web Site.

  4. At the bottom of the window, enter the name of the folder where you want to keep the pages of the Web site.

    For example:

    • If you are creating a file system Web site, type C:\SampleSite.

    • If you have IIS installed and you are creating an HTTP Web site, type https://localhost/SampleSite.

  5. Click OK.

    Visual Studio creates the Web site project with default folders and files, and opens a new page named Default.aspx.

Creating a Shared Class

You can create reusable classes by keeping them in a folder named App_Code. Visual Studio monitors the App_Code folder and when new class files are added, makes the components available to the rest of the code in your application. By default, the classes in the App_Code folder are compiled into a single assembly at run time.

Note

You should put only classes (and other supported shared types) into the App_Code folder. Do not put pages, Web user controls, or other files that contain non-code elements into the App_Code folder.

To create an App_Code folder

  • In Solution Explorer, right-click the name of the Web site project, click Add ASP.NET Folder, and then click App_Code.

    You can now add the component to your site.

To create a shared class in the App_Code folder

  1. In Solution Explorer, right-click App_Code, and then click Add New Item.

    Note

    Be sure to create the new item in the App_Code folder, not in the root folder of the Web site.

  2. In the list of templates, select Class.

  3. In the Name box, type SampleClass1.

  4. Click Add.

    Visual Studio opens the new class file in the editor.

  5. Create a class that has a single property named testString by replacing the existing code in the file with the following code:

    Public Class SampleClass1
        private testStringValue As String
        Public Property testString as String
            Get
                return testStringValue
            End Get
            Set (Value as String)
                testStringValue = value
            End Set
        End Property
    End Class
    
    using System;
    public class SampleClass1
    {
        public SampleClass1() 
        {
        }
        private string testStringValue;
        public string testString 
        {
            get
            {
                  return testStringValue;
            }
            set
            {
                   testStringValue = value;
            }
        }
    }
    
  6. Save the file and then close it.

    Note that the file is not stored as a compiled file.

    Note

    When you work with shared classes in the App_Code folder, you do not have to save the components in order for Visual Studio to maintain a reference to the components. If the Web page and component are in the same programming language, Visual Studio maintains a reference to the component in memory. In this case, you are closing the file because you are finished with it.

Using the Shared Class

The next step is to use the shared class in an ASP.NET Web page. You can use the Default.aspx page that was created when you created the Web site project.

To use the shared class

  1. Open or switch to the Default.aspx page, and then switch to Design view.

    Note

    If you do not have a Default.aspx page, you can use another page. Alternatively, you can add a new page to the Web site project. To do this, in Solution Explorer, right-click the name of the project, click Add New Item, and then click Web Form. Under Installed Templates, enter the same programming language that you used for the component, and then click OK.

  2. From the Standard folder in the Toolbox, drag a TextBox control, Label control, and Button control onto the page.

    Note

    For this walkthrough, the layout of the page is not important.

  3. Double-click the Button control to create a Click handler for it.

    The click handler code might look similar to the following:

    Protected Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
    End Sub
    
    protected void Button1_Click(object sender, EventArgs e)
    {
    
    }
    
  4. In the handler, type the following:

    Dim sc As New
    
    SampleClass1 sc = new
    

    When you press SPACEBAR after typing New or new, Visual Studio displays a list of the available classes. The class that you created in the preceding section, SampleClass1, is included in the list.

  5. Finish the statement by typing SampleClass1 or by double-clicking it in the list, so that the statement reads as follows:

    Dim sc As New SampleClass1
    
    SampleClass1 sc = new SampleClass1();
    
  6. Press ENTER, and then type the following:

    sc.
    

    As soon as you type the period, Visual Studio again displays a list of members to help you select a member from the sample class.

  7. Finish the statement and the handler in the following manner:

    sc.testString = TextBox1.Text
    Label1.Text = sc.testString
    
    sc.testString = TextBox1.Text;
    Label1.Text = sc.testString;
    
  8. Save your files.

Testing the Page and Class

You can run the Web site to see that the shared class is working.

To test the page and component

  1. Open the Default.aspx page.

  2. Press CTRL+F5 to run the page.

  3. When the page appears in the browser, type something in the text box, and then click the button.

    Doing this sets a property in your simple class, which is then displayed in the Label control.

If you use Microsoft Windows Explorer to examine the directory where the Web site is located, you will see your page and the App_Code folder. Note that there is no .dll or other executable code in the App_Code folder or anywhere under the root of the Web site. Instead, Visual Studio has compiled the page and the shared class dynamically.

Next Steps

This walkthrough illustrates how to add shared classes to a Web site without compiling the components. You might want to use shared classes in different ways. For example, you might want to:

  • Work with a compiled component. If you have an assembly that you can use in the Web site, create a Bin folder, and then copy the .dll to Bin. You can then reference the assembly in your page in the same way that you referenced the component that you created in this walkthrough.

  • Create a component for data access. For more information, see Walkthrough: Data Binding to a Custom Business Object.

See Also

Concepts

ASP.NET Web Project Folder Structure

Shared Code Folders in ASP.NET Web Site Projects

Other Resources

ASP.NET Web Projects

ASP.NET Web Site Projects