For ASP. the use of the NET Server Control Render method may also have some questions about why you want to rewrite the Render method. I hope you can take a look at my example to understand ASP. NET Render method and the other two methods, and then really understand why under normal circumstances only need to override ASP. NET Render method.
We know that every time we write a control, we need to rewrite ASP. NET Render method. We found that many methods in the Control class can be rewritten, but we didn't rewrite them. We need to follow a principle and rewrite them when reload is required.
Let's take a look at the two methods related to the ASP. NET Render method.
- // Basic implementation of the RenderControl Method
- Public VoidRenderControl (HtmlTextWriter writer)
- {
- If(Visible)
- {
- Render (writer );
- }
- }
- // Basic implementation of the Render Method
- Protected Virtual VoidRender (HtmlTextWriter writer)
- {
- RenderChildren (writer );
- }
- // Basic implementation of the RenderChildren Method
- Protected Virtual VoidRenderChildren (HtmlTextWriter writer)
- {
- Foreach(Control cInControls)
- {
- C. RenderControl (writer );
- }
- }
I believe that the person who has read the book "ASP. NET Server Control development technology and Examples" must have read the above Code.
If you don't understand the above process (I don't necessarily understand it, but I hope my ideas will help you), I think there is a good way to understand the above process, share with you.
Now let's leave the above Code aside. Let's create a simple page and drag several widgets to the interface at will. Pay attention to the last three panel controls, as shown in figure
We know that each control has the Visible and EnableViewState attributes, and Visible is used to set whether the control is rendered.
Now we set the Visible attribute of the button control to flase. We can see the expected effect. Enable page tracing. This is very important.
Run this page on the server. You can see the following picture on the Control tree.
(1) System. Web. UI. LiteralControl
We can see that System. Web. UI. LiteralControl.
Note that you need to understand any other strings that do not need to be processed on the server.
How can this problem be understood? You can open the source code page of this running page. The code below shows that no, except for the server control, we have other elements (no other strings that need to be processed on the server ), contains spaces.
Example 1
- <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN"
-
- Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <Html xmlns =Http://www.w3.org/1999/xhtml">
- <Head> <title>
- Too many bytes have been created> 〉
- </Title>
- <Body>
- <Form name ="Form1"Method ="Post"Action ="Default1.aspx"Id ="Form1">
- <Div>
- <Input type ="Hidden"Name ="_ VIEWSTATE"Id ="_ VIEWSTATE"
- Value ="/WEPDwULLTExNTUxMDYxODdkZHVaWm47e5anDettRKviGvS0nDWQ"/>
- </Div>
-
- <Div>
- <Span id ="Label1"> Label </span> <br/>
- <Br/>
- <Input name ="TextBox1"Type ="Text"Id ="TextBox1"/> <Br/>
- <Br/>
- <Br/>
- <Br/>
- <Div id ="Panel1"Style ="Height: 50px; width: 125px ;">
- </Div>
- </Div>
- <Div>
-
- <Input type ="Hidden"Name ="_ EVENTVALIDATION"Id ="_ EVENTVALIDATION"
-
- Value ="/WEWAgK/5/ftbwls0blrbrvw7yrsp5 g/l4sJGPkKN/asFj2W"/>
- </Div> </form>
- </Body>
- </Html>
To make everyone better understand the System. web. UI. literalControl: Let's modify the HTML page. Note: The above code is the running HTML source code. instead of the source code, you should understand what I mean by the source code.
Let's modify the code. Note: I wrote the following labels without spaces in <form.... Let's look at the modified Code below.
Example 2
- <%@ Page Language ="C #"AutoEventWireup ="True"
- CodeFile ="Default2.aspx. cs"Inherits ="Default2"Trace ="True"%>
-
- <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN"
- Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <Html xmlns =Http://www.w3.org/1999/xhtml">
- <Head runat ="Server">
- <Title> untitled page </title>
- </Head>
- <Body>
- <Form id ="Form1"Runat ="Server">
-
- <Asp: Label ID ="Label1"Runat ="Server"Text ="Label">
-
- </Asp: Label> <asp: TextBox ID ="TextBox1"Runat ="Server">
-
- </Asp: TextBox>
-
- <Asp: Button ID ="Button1"Runat ="Server"Text ="Button"Visible ="False"/>
-
- <Asp: Panel ID ="Panel1"Runat ="Server"Height ="50px"Width ="125px"></ Asp: Panel>
-
- </Form>
- </Body>
- </Html>
Running Effect
No System is found between controls. web. UI. literalControl, because I have removed spaces. this also shows that if the code is messy, the speed will be affected. now you should understand System. web. UI. what does LiteralControl mean.
(2) Let's continue to look at the Button1 in Figure 4. We will find that the number of bytes is 0, because we set the Visible value of Button1 to False, so this control is not displayed.
Next, let's understand this. We can see the RenderControl method again. If the Visible value is True, this control is displayed.
If (Visible)
Render (writer );
To understand this method, we will rewrite this method. Let's take the CreditCardForm3 Control described for the first time as an example.
We override the RenderControl method, copy all the code of the ASP. NET Render method to the RenderControl method, and then remove the Render method.
Then, use this control on the ASP. NET page to define its Visible value as False.
After running this example, you will find that the control is still rendered, because you have rewritten the RenderControl method to make the Visible value of the control invalid.
So we need to add a judgment.
If (Visible ){}
Otherwise, the content presented by this method does not have a Visible value. To better understand this, we override the RenderControl method of the base class.
Base. RenderControl (writer );
You will find that there are two controls during page rendering, one is output in the RenderControl method and the other is output in the Render method, because
Base. the RenderControl method calls the Render method. When the Visible attribute of the control is set to False, the output content of the Render method is hidden (not rendered, and the output content of the RenderControl method still exists. now you should understand the role of the RenderControl method.
If ASP. if the Visible attribute of the NET Server Control is set to true, the content of the server control is displayed to the page. Therefore, we do not override this method. generally, the control requires the Visible attribute unless otherwise specified.
(3) RenderChildren Method
Again, we can see that the drag-and-drop control is a child control of form1, and the panel control is a container control, because there is no drag-and-drop control below, any other displayed string is represented as System. web. UI. literalControl. You can drag several controls to the panel and run them again. The dragged controls are changed to the panel sub-controls. the most obvious test method is the Wizard control. Drag and Drop a Wizard control and test it again.
The RenderChildren method is used to determine whether the current control has child controls. If yes, the RenderControl method is used to determine whether the Visible value of the control is used to present the control. therefore, the RenderChildren method cannot be implemented when the Render method of the base class is not overwritten. the consequence is that the child control cannot be rendered.
Let's test it. we still use the CreditCardForm3 control as an example (Please comment out the content of the RenderControl method). When the RenderChildren method is not re-implemented, the sub-control content cannot be displayed. Please start the tracking
The child widget is rendered in bytes 0.
Because CreditCardForm3 inherits CreditCardForm2, the rewrite base class Render method will be output repeatedly. We can rewrite the RenderChildren method directly in the Render method. Then we can test it. Some changes will be found.
It is found that the byte of its child control is not 0, but 10
It indicates that its sub-controls still exist, but they are useless. Therefore, you can decide whether to rewrite the RenderChildren method based on your actual needs. Generally, the Render method will be rewritten, which is a little safer.
Okay. Now let's review the code that you just gave. do you understand it through the experiment above?
Steps for rendering a custom control (Note: The following three methods can be displayed, but we have already said that, for example, if you use HtmlTextWriter to output a custom control in advance in the RenderControl method, the Visible function is lost (maybe you don't need this function, then you can rewrite this method)
(1) RenderControl Method
First determine its Visible and then call the Render Method
(2) Render Method
Use HtmlTextWriter to output tagged characters and text and then call the RenderChildren method.
(3) RenderChildren Method
Determine whether the current control has a child control, and then call the RenderControl method to output the child Control Based on the Visible value of the Child control.
After learning about the above three methods, we will know that, in general, we do not need to rewrite the RenderControl method and RenderChildren method. so the most suitable method is to rewrite the Render method. I talked a lot about it. the purpose is to explain why the Render method needs to be rewritten.
Last time, I forgot to upload the code. I accidentally uploaded the dll file. Sorry. this time I wrote so much. I hope you can understand it. you can modify the code as appropriate, so that you can find more.
If you have any errors, please point out that we hope to talk with you more. ^_^
The use of the ASP. NET Render method in the ASP. NET Server Control will introduce you here. I hope you will have some knowledge about using the ASP. NET Render method to present the content of custom controls.
- Development of ASP. NET controls-how to trigger JavaScript scripts using UpdatePanel
- ASP. NET control development tips: disable unnecessary functions of the base class
- Analysis on the use of HtmlTextWriter class in ASP. NET control development skills
- Analysis on ComboBox display of ASP. NET control development skills
- Analysis of Custom Controls Based on ASP. NET control development