In many cases, when developing, we need to dynamically add controls, including HTML controls and ASP. NET controls.
JS generate HTML controls
Function addproperty ()
{
VaR COUNT = Document. getelementbyid ('hidcount'). value;
VaR STR = "<br> property name: <input type = \ "text \" id = \ "txtproperty" + Count + "\" name = \ "txtproperty" + Count + "\"/> & nbsp; & nbsp; attribute description: <input type = \ "text \" id = \ "txtdesc" + Count + "\" name = \ "txtdesc" + Count + "\"/> ";
Count ++;
Document. getelementbyid ('hidcount'). value = count;
Document. getelementbyid ('catepro'). innerhtml + = STR;
}
To obtain the value of the generated control, you must specify its ID. You can first place a hiddenfield control on the page and specify the default value to 0. When a control is dynamically added, you can use the JS script to change its value. The script is as above.
<Asp: hiddenfield id = "hidcount" value = "0" runat = "server"/>
In the page background, you can obtain the value as follows:
Int COUNT = int. parse (this. hidcount. Value); // gets the number of generated controls, stores them in the hiddenfield on the page, and obtains the ID.
For (INT I = 0; I <count; I ++)
{
String propname = string. empty;
String propdesc = string. empty;
If (! String. isnullorempty (request ["txtproperty" + I]) // you can check whether a property exists.
{
Pim. model. cateproperty model = new Pim. model. cateproperty ();
Propname = request ["txtproperty" + I]. tostring (); //, obtain the value of the generated control
Propdesc = request ["txtdesc" + I]. tostring ();
//......
}
}
Ii. dynamically generate ASP. NET Server controls and obtain their values
Private void loadproperty (INT cateid)
{
Htmlgenericcontrol span = new htmlgenericcontrol ();
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Stringbuilder strhtml = new stringbuilder ();
If (I % 2 = 0)
{
Strhtml. append ("<div> ");
Strhtml. append ("<Div class = \" layout1 \ "> ");
}
Else
{
Strhtml. append ("<div> ");
}
Textbox TXT = new Textbox ();
Label LBL = new label ();
Hiddenfield HDF = new hiddenfield ();//
LBL. ID = "lblprop" + I. tostring ();
LBL. Text = Ds. Tables [0]. Rows [I] ["propname"]. tostring () + ":";
TXT. ID = "txtprop" + I. tostring ();
HDF. ID = "hdfprop" + I. tostring ();//
LBL. enableviewstate = true;
TXT. enableviewstate = true;
HDF. enableviewstate = true ;//
Literalcontrol lc = new literalcontrol (strhtml. tostring ());
LC. enableviewstate = true;
Span. Controls. Add (LC );
Span. Controls. Add (LBL );
Span. Controls. Add (txt );
Span. Controls. Add (HDF );//
If (I % 2 = 0)
{
Span. Controls. Add (New literalcontrol ("</div> "));
}
Else
{
Span. Controls. Add (New literalcontrol ("</div> "));
}
}
}
Dynamically generated controls. When you click the page button, the page is refreshed.
If (! Ispostback)
{
}
In the pageload method, even if the data cannot be obtained from every dynamic load, and the dynamically generated control disappears. net lifecycle, We will generate the control in the oninit () method,
This method is rewritten.
Protected override void oninit (eventargs E)
{
Loadproperty (cateid); // execute the dynamic add control method in oninit to save its status view.
}
If these dynamically generated controls are generated, they need to be taken from the database and assigned values. You can assign values in pageload,
In this case, you can use (! Ispostback) without losing its value.
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
If (! String. isnullorempty (request ["productid"])
{
String productid = request ["productid"]. tostring ();
Int parentid = int. parse (parentid );
Loaddata (productid, parentid); // you can assign a value to it using a method... The specific implementation is omitted.
}
}
}