This article describes a way to create a control in the background of C # and get a value. Share to everyone for your reference. The implementation method is as follows:
Front Code:
The code is as follows:
<form id= "form1″runat=" Server >
<div>
<div class= "Item" >
Please input a number:
<asp:textbox runat= "Server" cssclass= "item" id= "Txttextcount" ></asp:TextBox>
<asp:button runat= "Server" id= "btncreate" text= "Create TextBox List" validationgroup= "CreateTextbox"
onclick= "Btncreate_click"/>
<asp:button runat= "Server" id= "Btnok" text= "Get Control Value" validationgroup= "Showlistcontent"
onclick= "Btnok_click"/>
</div>
<div runat= "Server" id= "Divcontrols" class= "Item" >
</div>
<div runat= "Server" id= "Divmessage" >
</div>
</div>
</form>
Background code:
The code is as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (this. IsPostBack)
{
int txtcount = Int. Parse (Txttextcount.text);
Note: Each time you postback, you need to dynamically create a TextBox
Createtextboxlist (Txtcount);
}
}
<summary>
Create textbox List
</summary>
<param name= "num" >textbox list count</param>
private void createtextboxlist (int num)
{
HtmlGenericControl Div;
HtmlGenericControl span;
TextBox txt;
RegularExpressionValidator Rev;
for (int i = 0; i < num; i++)
{
Create Div
div = new HtmlGenericControl ();
Div. TagName = "DIV";
Div.id = "Divtextbox" + i.tostring ();
Div. Attributes["class"] = "item2";
Create 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 to 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!";
Adding controls to a container
Div. Controls.Add (span);
Div. Controls.Add (TXT);
Div. Controls.Add (rev.);
DivControls.Controls.Add (DIV);
}
}
protected void Btncreate_click (object sender, EventArgs e)
{
txttextcount.enabled = false;
btncreate.enabled = false;
}
protected void Btnok_click (object sender, EventArgs e)
{
TextBox txt;
HtmlGenericControl span;
StringBuilder Sbresult = new StringBuilder ();
int txtcount = Int. Parse (Txttextcount.text);
Traversal gets the text value in the dynamically created textbox
for (int i = 0; i < Txtcount; i++)
{
Note: The dynamically created TextBox must be obtained through the upper container to get 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}.<br/>", i + 1, txt.) Text.trim ());
}
}
Traversal gets the text value in the dynamically created textbox
for (int i = 0; i < Txtcount; i++)
{
Note: The dynamically created TextBox must be obtained through the upper container to get the ViewState content
span = Divcontrols.findcontrol ("Spantextbox" + i.tostring ()) as HtmlGenericControl;
if (span! = NULL && span. Innertext.trim (). Length > 0)
{
Sbresult.appendformat ("Url address{0}: {1}.<br/>", i + 1, span.) Innertext.trim ());
}
}
divmessage.innerhtml = Sbresult.tostring ();
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Methods for creating controls and getting values in the background
This address: http://www.paobuke.com/develop/c-develop/pbk23156.html
Related content C # web crawler code share C # Simple crawl tool C # TreeView infinite Directory tree Implementation method C # using Icsharpcode.sharpziplib to achieve online compression and decompression C # extracting hyperlinks in Web pages link and text parts of the method
Tips for using reflection to traverse an object's properties and values in C # The difference between foreground and background threads in C # class instance of C # mailbox Mail send Class C # get hash encryption generate random security code
C # Methods for creating controls and getting values in the background