protected void page_load(object sender, eventargs e)
{
if (this.ispostback)
{
int txtcount = int.parse(txttextcount.text);
// 注意:每次postback时,都需要重新动态创建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++)
{
//创建div
div = new htmlgenericcontrol();
div.tagname = "div";
div.id = "divtextbox" + i.tostring();
div.attributes["class"] = "item2";
//创建span
span = new htmlgenericcontrol();
span.id = "spantextbox" + i.tostring();
span.innerhtml = "url address" + (i + 1).tostring() + ":";
//创建textbox
txt = new textbox();
txt.id = "txt" + i.tostring();
txt.cssclass = "input";
//创建格式验证控件,并且将其关联到对应的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!";
//添加控件到容器
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);
//遍历获取动态创建的textbox们中的text值
for (int i = 0; i < txtcount; i++)
{
//注意:这里必须通过上层容器来获取动态创建的textbox,才能获取取viewstate内容
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());
}
}
//遍历获取动态创建的textbox们中的text值
for (int i = 0; i < txtcount; i++)
{
//注意:这里必须通过上层容器来获取动态创建的textbox,才能获取取viewstate内容
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();
}