[Implementing the ASP. Net Control as day15] Compound Control hiding

Source: Internet
Author: User

In the previous article, we used compositecontrol to implement the tbtoolbar control. In this article, we will test the compositecontrol to explain some precautions for using composite controls.

ProgramCodeDownload: ASP. NET Server Control-day15.rar

I. Timing of creating child controls for composite controls

I still remember that when we introduced the compositecontrol class, we mentioned that the compositecontrol class will ensure that when we access the child control, its child control will be created in advance; that is, when we use the CONTROLS attribute to access the child control, the createchildcontrols method will be executed to ensure that the child control is created in advance. Let's take a look at the write method of the CONTROLS attribute of the compositecontrol class to understand the reason. In the access to compositecontrol. when the CONTROLS attribute is set, it will first execute control. the ensurechildcontrols method, while the ensurechildcontrols method determines whether the child control has been created. If not, the createchildcontrols method is executed, this is why compositecontrol has the identification method to ensure that the child control is established in advance.

The compositecontrol. Controls attributes are as follows:

 
Public Overrides Readonly PropertyControlsAsControlcollection
Get
 
Me. Ensurechildcontrols
 
Return Mybase. Controls
 
End Get
 
EndProperty

 

 

The method of control. ensurechildcontrols is as follows:

 
Protected Overridable SubEnsurechildcontrols ()
If(Not Me. ChildcontrolscreatedAndalso Not Me. Flags. Item (& h100 ))Then
 
Me. Flags.Set(& H100)
 
Try 
 
Me. Resolveadapter
 
If(Not Me. _ AdapterIs Nothing)Then
 
Me. _ Adapter. createchildcontrols
 
Else
Me. Createchildcontrols
 
End If
 
Me. Childcontrolscreated =True
 
Finally
 
Me. Flags. Clear (& h100)
 
End Try
 
End If
 
EndSub

 

 

Ii. Hidden compound controls

Taking the tbtoolbar control as an example, we have written some test cases to illustrate the problem of composite controls. Before writing a test case, modify the tbtoolbar control, overwrite the loadviewstate and saveviewstate methods, and store the items attribute in viewstate to maintain the state.

''' <Summary>
 
''' The viewstate also controls the metadata of the listener.
 
''' </Summary>
 
''' <Param name = "savedstate"> You must restore the original zookeeper control. </Param>
 
Protected Overrides SubLoadviewstate (ByvalSavedstateAs Object)
 
If Not(SavedstateIs Nothing)Then
 
'Load state from the array of objects that was saved;
'Savedviewstate.
 
DimMystateAs Object() =Ctype(Savedstate,Object())
 
 
 
If Not(Mystate (0)Is Nothing)Then
 
Mybase. Loadviewstate (mystate (0 ))
 
End If
 
 
If Not(Mystate (1)Is Nothing)Then
 
Fitems =Ctype(Mystate (1), tbtoolbaritemcollection)
 
End If
 
End If
 
End Sub
 
 
 
''' <Summary>
 
'''Controls whether or not the listener is saved to viewstate.
 
''' </Summary>
''' <Returns> contains the objects that control the token currently used when the token expires. </Returns>
 
Protected Overrides FunctionSaveviewstate ()As Object
 
DimBasestateAs Object=Mybase. Saveviewstate ()
 
DimMystate (1)As Object
 
Mystate (0) = basestate
 
Mystate (1) =Me. Items
 
ReturnMystate
 
EndFunction

 

 

 

 

On the test page, place the test 1, Test 2, and PostBack buttons as follows.

Test 1 button: Add a button directly in the tool column.
Test 2 button: Use findcontrol to get the tool column, and then add a button in the tool column.
"PostBack" button: simply execute PostBack without writing program code.

 

The program code for the three buttons is as follows.

 
Protected SubButton#click (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesButton1.click
 
DimOitemAsTbtoolbaritem
 
 
 
'Add a new worker
 
Oitem =NewTbtoolbaritem ()
 
Oitem. Text ="New by hour"
 
Oitem. Key ="Newbutton"
 
Tbtoolbar1.items. Add (oitem)
Me. Response. Write ("" Zookeeper 1 "by region")
 
End Sub
 
 
 
Protected SubButton2_click (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesButton2.click
 
DimOitemAsTbtoolbaritem
 
DimObuttonAsButton
 
 
 
'Obtain the id = "add" by using the findcontrol statement first.
Obutton = tbtoolbar1.findcontrol ("Add")
 
 
 
'Then add the new worker
 
Oitem =NewTbtoolbaritem ()
 
Oitem. Text ="New by hour"
 
Oitem. Key ="Newbutton"
 
Tbtoolbar1.items. Add (oitem)
 
Me. Response. Write ("" Zookeeper II "by region")
 
End Sub
 
 
Protected SubButton3_click (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesButton3.click
 
'Simple PostBack, no program allowed
 
Me. Response. Write ("" PostBack "by category")
 
EndSub

 

Case 1: Execute the test 1 button and add a button directly in the tool column.

When you press test 1, you can add the new button to the tool column.

 

Case 2: run the test 2 button. Use findcontrol to get the tool column, and then add a button in the tool column.

Re-execute the program. When you press test 2, you will find that the tool column is not included in the new button?

 

Press the "PostBack" button to display the button we just added in the tool column.

Why is this strange phenomenon? In fact, the reason is very simple, because the findcontrol will access the CONTROLS attribute, and the subcontrol has been created. However, if you add a new button with the items Attribute before, it will no longer recreate the subcontrol, as a result, no new button is added immediately. However, the items property will be stored in viewstate, so when you execute the "PostBack" button, the button we just added will appear.

 

Iii. Solution

To solve the problem of "Test 2", you only need to override the render method of the tbtoolbar control, execute the recreatechildcontrols method before render, and forcibly recreate the child control.

 
''' <Summary>
 
''' Override the render method.
 
''' </Summary>
 
Protected Overrides SubRender (ByvalWriterAsSystem. Web. UI. htmltextwriter)
Me. Recreatechildcontrols ()
 
Mybase. Render (writer)
 
EndSub

 

 

 

If you run test 2 again, you will find that the execution result is normal.

 

Iv. Conclusion

Execute the recreatechildcontrols method before the render of the Composite Control to force re-build the sub-control, but this will lead to another problem, that is, when you directly access the sub-control to modify the attributes of the sub-control, once the subcontrol is re-built in render, the status of the subcontrol is re-created. Therefore, pay special attention to this situation. In addition, the composite control may repeatedly execute the action of creating sub-control, which is not efficient in execution.

Note: This article is also published in "the first it state help tie Ren Competition". If you think this articleArticleIt is helpful to you. Remember to add popularity when you connect to this article ^
Http://ithelp.ithome.com.tw/question/10012425

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.