Partager via


Troubleshooting Design View Rendering

Several common issues can arise when you attempt to open a file in the Web Forms designer that cannot be rendered correctly in the designer. Design-view rendering issues occur most frequently when you are working with a file that was originally created as a code-behind Web Forms page outside of Visual Studio. However, these issues can occur under a variety of circumstances. Typically, the problem involves attributes of the @ Page directive in a Web Forms page, or of the @ Control directive in a Web Forms user control.

NoteNote:

The Web Forms designer is the registered editor for files with an extension that matches that of the file you are working with. If you do not want to use the Web Forms designer to work with a file, change the file's extension. Alternatively, in Solution Explorer you can right-click the file, choose Open With, and select a different editor.

Issues with the Src Attribute

An error might occur if you have a Src attribute in the @ Page or @ Control directive. This attribute is not supported in the Web Forms designer. To fix this error:

  • Remove the Src attribute. In its place, add:

    • A CodeBehind attribute that references the name of a code-behind file in the project.

    • An Inherits attribute that references the class name represented by the code-behind file. (This class should in turn inherit from the System.Web.UI.Page class.)

    NoteNote:

    If there is not already a code-behind file and class, you can have the Web Forms designer generate one. After removing the Src attribute, set the Inherits attribute to an empty string ("") and the CodeBehind attribute to the name of the code-behind file to generate (formname.aspx.vb or formname.aspx.cs). Save the file and close it. In Solution Explorer, right-click the .aspx file and choose View Code. When you save the code-behind file, the designer will generate an appropriate class file. Open the file again. Perform any gesture that generates code in the code-behind file (such as adding a control or double-clicking a control to create an event handler) and then save the file. The designer sets the Inherits attribute of the Web Forms page appropriately.

Alternatively, you can remove the Src, CodeBehind, and Inherits attributes and treat the file as a single-file Web Forms page. For more information, see Working with Single-File Web Forms Pages in Visual Studio .NET.

Issues with the CodeBehind Attribute

The CodeBehind attribute in the @ Page or @ Control directive references the name of a file that contains the class for the page. For example, if you create a Web Forms page in Visual Studio named WebForm1, the CodeBehind attribute will point to WebForm1.aspx.vb or WebForm1.aspx.cs.

The CodeBehind attribute is used only at design time; it tells the Web Forms designer where to find the page class so that the designer can create an instance of it for you to work with.

Problems associated with the CodeBehind attribute can include:

  • The referenced file is not available. Either the file does not exist or it has a different name.

  • The class in the referenced file is inherited from a base class that is not referenced in the project. Normally, the class for your Web Forms page is inherited from the System.Web.UI.Page class, but if you are working with a user control or a Web Forms page for another platform, such as a mobile device, the base class might be different.

The class in the referenced file inherits from a base class in the project that has not been compiled.

Issues with the Inherits Attribute

The Inherits attribute in the @ Page or @ Control directive references the name of a class (not a file) from which the page inherits at run time. For more information about the inheritance model for Web Forms pages, see Web Forms Code Model.

Problems with the Inherits attribute arise only if there is not also a CodeBehind attribute. This might be true, for example, in a single-file Web Forms page. If there is no CodeBehind attribute, only then does the designer use the class referenced in the Inherits attribute to create an instance of the page at design time.

NoteNote:

If there is a CodeBehind attribute, the designer will ignore the Inherits attribute, even if the Inherits attribute is pointing to a class that is not available. In that case, you can work with the page in the designer, but it will not run.

The most common problem associated with the Inherits attribute is that it points to a class that is not available in the project or in a referenced assembly.

See Also

Concepts

Design View