Adding controls in the folia

xiaoxiao2021-03-06  96

During the actual development process, it is often necessary to load some server controls or user controls according to actual needs, usually divided into static and dynamic loading. The following mainly explains how to dynamically load controls, dynamically load controls in the foliage often With the PlaceHolder control as a container to load these dynamic loaded controls. This example is a new control Sub btnaddControl_Click (Sender As Object, E AS Eventargs) DIM TXTBOX AS TEXTBOX = New TEXTBOX 'Clear Placeholder All control items will then add the generated TextBox control to the PlaceHolder1.controls.clear () PlaceHolder1.controls.add (txtbox) this is basically a TextBox control, when point "btnaddControl". You can see a textbox! 'Can define the various properties of the loaded TextBox (Note To add controls, set it to the properties) txtbox.id = "mytextbox" txtbox.backcolor = system.drawing.color.lightgreen txtbox .text = "this is a textbox!" ViewState ("txtboxControl") = trueEnd Sub will dynamically load a TextBox control at the point button, but there will be a problem: that is, when refreshing the leaves, this TextBox is I will not see. This is because dynamic loaded controls do not automatically save his ViewState status on the leaves. If you can't think of this problem, you must implement it in page_load () events, as follows: SUB Page_Load (Sender AS Object, e as evenetargs' decisions whether there is ViewState of previously generated controls, existence, re-generate controls and automatically load it previous ViewState status if ViewState ("txtboxControl") <> Nothing the Dim txtbox as textbox = New TextBoxPlaceHolder1.Controls.Add (txtBox) txtBox.ID = "MytextBox" End IfEnd Sub dynamic load user controls are the same way (dim Mycontrol as control = page.loadcontrol ( "header.ascx") PlaceHolder1.Controls.Add (Mycontrol) -------------------------------------------------- ----------------------

转载请注明原文地址:https://www.9cbs.com/read-100615.html

New Post(0)