How to solve the "error occurred when rendering the control" Problem

Source: Internet
Author: User

When creating a control, you may encounter an unhandled exception in "An error occurred while rendering the control. The object reference is not set to the instance of the object ." This error may occur only because the runat = "server" label is not set. Check this item first .)

However, there are no errors during compilation/runtime.

Analysis

We call it "design time" to distinguish it from "compile time"/"runtime ".

Design Time: In the ASP. NET environment, when we use the web designer in Visual Studio for editing. The direct understanding is that when the ASPX page is switched to "design.

Compile time: The direct understanding is that when you compile, usually the errors at this time are caused by type checks, parameter matching, and other explicit errors that can be directly restricted by syntax constraints.

Runtime: You can directly preview or run the program. Generally, errors at this time are composed of specific exceptions and logical errors.

Let's analyze the display of the control during design. during design, vs intelligently simulates the appearance of the control during running, controls are rendered in a certain order and the current output is formed. According to the standard, we should have performed the output operation on the Control in render or rendercontents (in fact, other operations are acceptable, but we usually do not, or more "error occurred while rendering the control" exceptions mainly come from render or rendercontents ).

The error message "the object reference is not set to the instance of the object ." In this case, an instance of one or more objects is used without initial values.

Let's look at our code:

protected override void RenderContents(HtmlTextWriter writer){    UpButton.Text = Page.Server.HtmlDecode(UpButton.Text);    DownButton.Text = Page.Server.HtmlDecode(DownButton.Text);    base.RenderContents(writer);}

This control requires an up button and a down button during design. Two Special punctuation marks must be used for up and down, respectively, the two symbols must be correctly rendered by setting the code numbers shown below:

private string upButtonText = "∧";private string downButtonText = "∨";

These two symbols are compiled by the htmlencode method on the page before being rendered, but these two special symbols can only be rendered by direct output, that is to say, after htmlencode, the special mark can only be output in the form of text & and; & or, rather than the up and down arrows. In this case, we need to introduce its reverse method page. server. htmldecode decoding. Note that the page instance is used here. This instance is not empty only when the page actually exists. Otherwise, subsequent operations will be performed on null, in this case, "the instance of the object is not referenced." This error occurs.

DESIGN: as we mentioned earlier, the design only simulates the page rendering process, and the page does not actually exist. Therefore, the page object instance will be empty at this time, and subsequent calls will cause exceptions.

Assume that we only use this method to process the currently required behavior. Therefore, when we call page, it will certainly lead to null objects and subsequent operations will be abnormal. At this time, we introduce the concept of "Design Pattern" (instead of designpattern but designMode). designMode is a protected (protected) attribute of the control class, And it obtains a value, indicates whether the component is in design mode. The design pattern here is equivalent to the design concept.

Therefore, we can transform the code into the following form:

protected override void RenderContents(HtmlTextWriter writer){    if (!DesignMode)    {        UpButton.Text = Page.Server.HtmlDecode(UpButton.Text);        DownButton.Text = Page.Server.HtmlDecode(DownButton.Text);    }    base.RenderContents(writer);}

In this way, the page object instance is introduced only during non-design, so the design exceptions will be solved.

Summary

Therefore, when designing a control, especially when considering the display of the control, to avoid similar exceptions, we should consider that the control can obtain sufficient resources during design, for items that fail to obtain resources, we should explicitly differentiate them (the code above uses the designMode to determine whether it is the code to be executed during design ).

Extension

What we saw just now can be attributed to the exceptions caused by the failure to reference a specific instance during design, similar to this exception, database/File System reads, variables are not attached with initial values, and page-like attributes such as session and page are called. request. querystring. During page design, because some of the above solutions, especially the methods related to page calling, always exist on the page, we will not often see exceptions in them, when designing controls, we should pay more attention to them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.