Workarounds for errors when rendering controls in VS2010

Source: Internet
Author: User
An unhandled exception occurred while making the control prone to encountering "error rendering control". Object references are not set to an instance of an object. "Such an error, such as: (It is also possible that this error is caused simply because the runat=" server "label is not set, please check the item first.) )
However, there is no error at compile time/run time.

Analysis
The situation we call "design time" to distinguish it from "compile-time"/"Runtime."

Design time: In an ASP. NET environment, when we use the Web page designer for editing in Visual Studio. The direct understanding is when the ASPX page switches to "design".

Compile time: The direct understanding is that when you compile, usually this time the error is by the type check, parameter matching and so on explicitly can be directly through the syntax constraints of the error.

Runtime: The direct understanding is when you are previewing/running. Usually the error at this time is made up of specific exceptions and logic errors.
Let's analyze the performance of the control at design time, when our control is at design time, vs intelligently simulates the appearance of the output control at run time, the rendering of the control passes through a certain sequence of methods, and eventually the current output is formed. According to the standard, we should be the output of the control in render or rendercontents (in fact, other can be, but we usually do not do that, or more "error rendering control" exception is mainly from the render or rendercontents )。
The object reference is not set to an instance of the object from the wrong prompt. "From this point of view, in other words, an instance of one or more objects is used without assigning an initial value."

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);}

Because the control needs to have an up button and a down button at design time, each need to use two special punctuation marks up and down, and the two symbols need to be correctly rendered by the browser by setting the code number as shown below:

private string upbuttontext = "∧";p rivate string downbuttontext = "∨";

And these two symbols before being rendered by the page will be a HTMLEncode method compiled and then output, and these two special flags can only be rendered by direct output, that is, after HTMLEncode only the special flag can be output in the form of text ∧ ∨ instead of outputting up and down arrows. At this time we need to introduce its inverse method Page.Server.HtmlDecode to decode, notice here we use the page instance, the instance is not empty only if the pages are real, otherwise the subsequent operation will be null operation, and such operation will show " Object references are not set to an instance of an object. "Such a mistake.
Design time: As we mentioned just now, design is just a process of simulating the rendering of a page, and the page actually does not exist. So at this point the instance of the Page object will be empty, and subsequent calls will throw an exception.

Assuming that we only have this method to handle the currently required behavior, we will cause the object to be NULL when we call the page and cause the subsequent operation to be an exception. This time we introduce the concept of "design pattern" (not Designpattern but designmode), DesignMode is a protected (protected) property of the control class, it gets a value, Used to indicate whether the component is currently in design mode. The design pattern here is also equivalent to the design-time concept.

So 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);}

This allows us to introduce an instance of the Page object only when it is not designed, so the design-time exception will be solved.

Summarize

Therefore, in designing the control, especially when considering the control rendering, in order to avoid the occurrence of similar exception, we should consider that the control can get enough resources at design time, for the item that cannot get the resource, We should explicitly differentiate it (for example, using DesignMode in the code above to determine if the code is to be executed at design time).

Extended

The situation we have just seen can be attributed to exceptions caused by the inability to refer to a specific instance at design time, such as a database/filesystem read, a variable without an initial value, a Page-like property such as session,page.request.querystring, and so on. In the design of the page because the above part of the scheme, especially called to page related methods because the page will always exist, so we do not often see them out of the exception, in the design of the control we should pay more attention to.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

Related Article

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.