Export (0) Print
Expand All

Walkthrough: Publishing a Custom Web Control

You can create a custom web control and distribute it as a VSIX extension. You can also create a web control project template to use as the basis for more web controls.

To distribute a VSIX extension, we recommend that you add it to the Visual Studio Gallery website because developers can use Extension Manager to browse there for new and updated extensions. You can also distribute an extension by putting it on a different server, or by burning it on a CD or other media.

This walkthrough, which is one of two related walkthroughs, teaches how to create a custom web control and then distribute it. The other walkthrough, Walkthrough: Publishing a Web Control Project Template, teaches how to create and distribute a web control project template.

This walkthrough contains these sections:

  • Creating a Custom Web Control

  • Testing the Web Control

  • Preparing the Web Control for Distribution

  • Publishing the Web Control to the Visual Studio Gallery

  • Installing the Web Control from the Visual Studio Gallery

  • Removing the Web Control

To complete this walkthrough, you must understand web controls and know how to create projects, set project properties, and use the Visual Studio experimental instance. Both Visual Studio 2010 and the Visual Studio 2010 SDK must be installed on the computer.

To create a custom web control when no web control project template is available, use one of the extensibility control project templates that are included in the Visual Studio SDK. Then, modify the project.

To create a control project

  1. In Visual Studio, on the File menu, point to New and then click Project.

  2. In the New Project dialog box, in the left pane, expand Visual C# and then click Extensibility. In the center pane, click Windows Forms Toolbox Control. In the Name box, type MyWebControls. Select Create directory for solution. Click OK.

  3. In Solution Explorer, right-click the MyWebControls solution, point to Add and then click New Project.

  4. In the New Project dialog box, in the left pane, expand Visual C# and then click Web. In the center pane, click ASP.NET Server Control. In the Name box, type Temp. Click OK.

To merge the project files

  1. In Solution Explorer, in the Temp project, right-click ServerControl1.cs and rename it ColorTextControl.cs.

  2. In the MyWebControls project, delete ToolboxControl.cs.

  3. In the Temp project, copy ColorTextControl.cs and paste it in the MyWebControls project.

  4. Remove the Temp project from the solution.

    Solution

To merge the project references

  1. In Solution Explorer, in the MyWebControls project, right-click the References folder and then click Add Reference. In the Add Reference dialog box, on the .NET tab, select the following references and then click OK to add them to the project:

    • Microsoft.CSharp

    • System.Data.DataSetExtensions

    • System.Web

    • System.Xml.Linq

  2. In the project, in the References folder, remove each of the following references from the project:

    • System.Data

    • System.Windows.Forms

    • System.Xml

    References

To create the web control

  1. In Solution Explorer, in the MyWebControls project, double-click ColorTextControl.cs to open it in the editor.

  2. Change the namespace value to MyWebControls.

  3. In the ToolboxData attribute, replace both occurrences of ServerControl1 with ColorTextControl.

    These values specify the opening and closing tags that are generated for the control when it is dragged from the Toolbox to a web page at design time. They must match the name of the control class, which is also the name of the control that will appear in the Toolbox.

  4. Add a ProvideToolBoxControl attribute to the control class.

    The first argument to this attribute is the name of the assembly, which is the same as the namespace when the project is generated. The color text control will appear in a category that has this name in the Toolbox.

    The beginning of the control class source code should now resemble the following code.

    namespace MyWebControls
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:ColorTextControl runat=server></{0}:ColorTextControl>")]
        [ProvideToolboxControl("MyWebControls", false)]
        public class ColorTextControl : WebControl
        {
    
    
  5. Replace the get method of the default Text property with the following code.

    get
    {
        String s = (String)ViewState["Text"];
        return "<span style='color:green'>" + s + "</span>";
    }
    

    This wraps the text in a span tag that colors it green.

  6. In the Properties folder, double-click AssemblyInfo.cs to open it in the editor.

  7. Replace the AssemblyVersion revision number with an asterisk (*).

    [assembly: AssemblyVersion("1.0.0.*")] 
    
    

    Custom Toolbox controls are cached and refreshed only when the assembly signature changes. By incrementing the assembly version revision number with every build, the cache is always refreshed.

Before you distribute the web control, test it in an experimental instance of Visual Studio.

To test the web control

  1. In Visual Studio, press F5 to open an experimental instance of Visual Studio.

  2. On the Tools menu, click Extension Manager. The MyWebControls extension should appear in the center pane and be enabled.

    MyWebControls Extension in Visual Studio Exp
  3. Close Extension Manager.

  4. In the New Project dialog box, in the left pane, expand Visual C# and then click Web. In the center pane, use the ASP.NET Web Application template to create a project.

  5. In the project, open Default.aspx. Make sure that Source view is displayed.

  6. Open the Toolbox. The ColorTextControl control should be displayed in the MyWebControls category.

    MyWebControls ColorTextControl in Toolbox
  7. Drag a ColorTextControl somewhere in the body tag of the web page.

  8. In the ColorTextControl tag, add a Text attribute and make its value Think Green!. The resulting tag should resemble the following one.

    <cc1:ColorTextControl ID="ColorTextControl1" Text="Think Green!" runat="server" />
    
    
  9. Press F5 to start the ASP.NET Development Server.

    The display of the ColorTextControl should resemble the following picture.

    Think Green screen shot
  10. Close the ASP.NET Development Server.

  11. On the Tools menu, click Extension Manager.

  12. Select MyWebControls, and then click Uninstall.

  13. Close Extension Manager.

  14. Close the experimental instance of Visual Studio.

Before you publish your web control to the Visual Studio Gallery, add an icon to help identify it in Extension Manager and an image to illustrate what it does.

To add identifying images to the web control

  1. If you already have an icon file, add it to the project as an existing item. Otherwise, create an icon file and then add it. This walkthrough uses an icon file named Color.bmp, which shows the letter A on a yellow field.

    Control icon
  2. If you already have an image file, add it to the project as an existing item. Otherwise, create an image file, for example, a screen capture, and then add it. This walkthrough uses an image file named ScreenShot.bmp, which shows the control as it appears in a web page.

    Custom control screen shot
  3. Open source.extension.vsixmanifest.

  4. In the Description box, change the value to Color Text Web Control.

  5. Set Icon to Color.bmp and Preview Image to ScreenShot.bmp.

  6. You can change Author and Version if you want to.

  7. Save source.extension.vsixmanifest and close it. Rebuild the project as a Release build.

The web control is now ready to be published to the Visual Studio Gallery.

To publish the web control to the Visual Studio Gallery

  1. In a web browser, open the Visual Studio Gallery website.

  2. In the upper-right corner, click sign in.

  3. Use your Windows Live ID to sign in. If you do not have a Windows Live ID, you can create one here.

  4. Click Upload.

  5. In Step 1: Extension Type, select Control and then click Next.

  6. In Step 2: Upload, select I would like to upload my control. The Select your control box appears.

  7. Click Browse and then, in the \bin\Release folder of the project, select MyWebControls.vsix. Click Next.

  8. In Step 3: Basic Information, information from the source.extension.vsixmanifest file is displayed.

  9. Set Category to ASP.NET and Tags to toolbox, web control. You can also type a more detailed description.

  10. Read the Contribution Agreement and agree to it, and then in the box, type the text that is displayed.

  11. Click Create Contribution. This displays a message that the page has not yet been published.

  12. Click Publish.

  13. Search the Visual Studio Gallery for MyWebControls. The listing for the MyWebControls extension should appear.

    Control listing in the Visual Studio gallery

Now that the web control is published, install it in Visual Studio and test it there.

To install the web control in Visual Studio

  1. In Visual Studio, on the Tools menu, click Extension Manager.

  2. Click Online Gallery and then search for MyWebControls. The listing for the MyWebControls extension should appear.

  3. Click Download. After the extension is downloaded, click Install.

  4. To complete the installation, restart Visual Studio.

  5. Create an ASP.NET web application project.

  6. In the project, open Default.aspx. Make sure that Source view is displayed.

  7. Open the Toolbox. The ColorTextControl control should be displayed in the MyWebControls category.

You can remove the web control from the Visual Studio Gallery and from your computer. These removals are required if you want to complete the related walkthrough, Walkthrough: Publishing a Web Control Project Template.

To remove the web control from the Visual Studio Gallery

  1. Open the Visual Studio Gallery website.

  2. In the upper-right corner, click My Contributions. The listing for MyWebControls listing is displayed.

  3. To remove the web control, click Delete.

To remove the web control from your computer

  1. In Visual Studio, on the Tools menu, click Extension Manager.

  2. Select MyWebControls and then click Uninstall.

  3. To complete the uninstallation, restart Visual Studio.

Walkthrough: Publishing a Web Control Project Template shows how to create a web control project template that is based on the web control from this walkthrough, and also shows how to distribute the template.

Community Additions

ADD
Show:
© 2015 Microsoft