C#后台创建控件并获取值的方法
2021-07-21 11:03
本文实例讲述了C#后台创建控件并获取值的方法。分享给大家供大家参考。具体实现方法如下:
前台代码:
代码如下:
后台代码:
代码如下:
{
if (this.IsPostBack)
{
int txtCount = int.Parse(txtTextCount.Text);
// 注意:每次PostBack时,都需要重新动态创建TextBox
CreateTextBoxList(txtCount);
}
}
///
/// Create textbox list
///
///textbox list count
private void CreateTextBoxList(int num)
{
HtmlGenericControl div;
HtmlGenericControl span;
TextBox txt;
//RegularExpressionValidator rev;
for (int i = 0; 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
{
//注意:这里必须通过上层容器来获取动态创建的TextBox,才能获取取ViewState内容
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());
}
}
//遍历获取动态创建的TextBox们中的Text值
for (int i = 0; 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}.
”, i + 1, span.InnerText.Trim());
}
}
divMessage.InnerHtml = sbResult.ToString();
}
C#后台创建控件并获取值的方法
本文地址: http://www.paobuke.com/develop/c-develop/pbk23156.html
相关内容
上一篇:vue上传
下一篇:C#调用VB进行简繁转换的方法