How to add controls dynamically in Asp.net
When using Asp.net for development, you need to dynamically add controls to the page for some reasons. Of course, these controls can be common HTML tags or server-side controls that are unique to ASP. NET. Everyone is familiar with dynamically writing HTML Tag controls. This article focuses on how to dynamically write controls to the server and obtain the attributes of the server-side controls that are dynamically written when the page is PostBack to the server.
Here, I will use a demo to describe this application.
Requirements:
1. the user enters a value (for example, 5) on the UI, and the system dynamically loads the URL address field of this value for the user;
2. the URL address entered by the user must be verified in URL format;
3. After the user submits the input content, the system returns the submitted results.
The design is as follows:
1. CSS style settings:
Reference content is as follows:
<Style type = "text/CSS">
. Item
{
Margin: 10px;
Border-bottom: solid 1px # CCC;
}
. Item2
{
Margin: 5px;
}
. Input
{
Width: 200px;
}
</Style>
Content "validationgroup =" showlistcontent "<p runat =" server ">
2. Front-end Page code:
Reference content is as follows:
<Div>
<Div class = "item">
Please input a number:
<Asp: textbox runat = "server" cssclass = "item" id = "txttextcount"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server" controltovalidate = "txttextcount" validationgroup = "createtextbox" display = "dynamic"
Errormessage = "required to input content! "> </ASP: requiredfieldvalidator>
<Asp: regularexpressionvalidator id = "regularexpressionvalidator1" controltovalidate = "txttextcount" validationgroup = "createtextbox" display = "dynamic"
Runat = "server" errormessage = "only number is valid! "Validationexpression =" ^ d + $ "> </ASP: regularexpressionvalidator> & nbsp;
<Asp: button runat = "server" id = "btncreate" text = "create textbox list" validationgroup = "createtextbox"
Onclick = "btncreate_click"/> & nbsp;
<Asp: button runat = "server" id = "btnok" text = "Get textbox content" validationgroup = "showlistcontent"
Onclick = "btnok_click"/>
</Div>
<Div runat = "server" id = "divcontrols" class = "item"> </div>
<Div runat = "server" id = "divmessage">
</Div>
</Div>
The dynamically created textbox is loaded into divcontrols.
3. Background code:
Reference content is as follows:
///
/// Create textbox list
///
/// Textbox list count
Private void createtextboxlist (INT num)
{
Htmlgenericcontrol div;
Htmlgenericcontrol span;
Textbox txt;
Regularexpressionvalidator Rev;
For (INT I = 0; I <num; I ++)
{
// Create a div
DIV = new htmlgenericcontrol ();
Div. tagname = "Div ";
Div. ID = "divtextbox" + I. tostring ();
Div. attributes ["class"] = "item2 ";
// Create a span
SPAN = new htmlgenericcontrol ();
Span. ID = "spantextbox" + I. tostring ();
Span. innerhtml = "url address" + (I + 1). tostring () + ":";
// Create a textbox
TXT = new Textbox ();
TXT. ID = "TXT" + I. tostring ();
TXT. cssclass = "input ";
// Create a format validation control and associate it with the corresponding textbox
REV = new regularexpressionvalidator ();
Rev. ID = "Rev" + I. tostring ();
Rev. controltovalidate = TXT. ID;
Rev. Display = validatordisplay. Dynamic;
Rev. validationgroup = "showlistcontent ";
Rev. validationexpression = @ "(HTTP (s )? ://)? ([W-] +.) + [w-] + (/[w -./? % & =] *)? ";
Rev. errormessage = "invalid URL address! ";
// Add the control to the container
Div. Controls. Add (SPAN );
Div. Controls. Add (txt );
Div. Controls. Add (REV );
Divcontrols. Controls. Add (DIV );
}
}
Protected void page_load (Object sender, eventargs E)
{
If (this. ispostback)
{
Int txtcount = int. parse (txttextcount. Text );
// Note: the textbox needs to be dynamically created every time the PostBack is performed.
Createtextboxlist (txtcount );
}
}
Protected void btncreate_click (Object sender, eventargs E)
{
Txttextcount. Enabled = false;
Btncreate. Enabled = false;
}
Protected void btnok_click (Object sender, eventargs E)
{
Textbox txt;
Stringbuilder sbresult = new stringbuilder ();
Int txtcount = int. parse (txttextcount. Text );
// Retrieve the text value in the dynamically created textbox through Traversal
For (INT I = 0; I <txtcount; I ++)
{
// Note: you must use the upper-layer container to obtain the dynamically created textbox to obtain the viewstate content.
TXT = divcontrols. findcontrol ("TXT" + I. tostring () as textbox;
If (txt! = NULL & TXT. Text. Trim (). length> 0)
{
Sbresult. appendformat ("url address {0 }:{ 1 }.
", I + 1, TXT. Text. Trim ());
}
}
Divmessage. innerhtml = sbresult. tostring ();
}