C# · 12月 28, 2021

C#后台创建控件并获取值的方法

本文实例讲述了C#后台创建控件并获取值的方法。分享给大家供大家参考。具体实现方法如下:

前台代码:

复制代码 代码如下:<form id=”form1″ runat=”server”>
<div>
<div class=”item”>
Please input a number:
<asp:Text@R_634_2419@ runat=”server” CssClass=”item” ID=”txtTextCount”></asp:Text@R_634_2419@>

<asp:Button runat=”server” ID=”btnCreate” Text=”Create Text@R_634_2419@ List” ValidationGroup=”CreateText@R_634_2419@”
OnClick=”btnCreate_Click” />
<asp:Button runat=”server” ID=”btnOK” Text=”获取控件值” ValidationGroup=”ShowListContent”
OnClick=”btnOK_Click” />
</div>
<div runat=”server” id=”divControls” class=”item”>
</div>
<div runat=”server” id=”divMessage”>
</div>
</div>
</form>

后台代码:
复制代码 代码如下:protected void Page_Load(object sender,EventArgs e)
{
if (this.IsPostBack)
{
int txtCount = int.Parse(txtTextCount.Text);

// 注意:每次PostBack时,都需要重新动态创建Text@R_634_2419@
CreateText@R_634_2419@List(txtCount);
}
}
///<summary>

/// Create text@R_634_2419@ list
///</summary>
///<param name=”num”>text@R_634_2419@ list count</param>
private void CreateText@R_634_2419@List(int num)
{
HtmlGenericControl div;
HtmlGenericControl span;
Text@R_634_2419@ txt;
//RegularExpressionValidator rev;

for (int i = 0; i < num; i++)
{
//创建div
div = new HtmlGenericControl();
div.TagName = “div”;
div.ID = “divText@R_634_2419@” + i.ToString();
div.Attributes[“class”] = “item2”;

//创建span
span = new HtmlGenericControl();
span.ID = “spanText@R_634_2419@” + i.ToString();
span.InnerHtml = “Url Address” + (i + 1).ToString() + “:”;

//创建Text@R_634_2419@
txt = new Text@R_634_2419@();
txt.ID = “txt” + i.ToString();
txt.CssClass = “input”;

//创建格式验证控件,并且将其关联到对应的Text@R_634_2419@
//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)
{
Text@R_634_2419@ txt;
HtmlGenericControl span;
StringBuilder sbResult = new StringBuilder();
int txtCount = int.Parse(txtTextCount.Text);

//遍历获取动态创建的Text@R_634_2419@们中的Text值
for (int i = 0; i < txtCount; i++)
{
//注意:这里必须通过上层容器来获取动态创建的Text@R_634_2419@,才能获取取ViewState内容
txt = divControls.FindControl(“txt” + i.ToString()) as Text@R_634_2419@;

if (txt != null && txt.Text.Trim().Length > 0)
{
sbResult.AppendFormat(“Url Address{0}: {1}.<br />”,i + 1,txt.Text.Trim());
}
}
//遍历获取动态创建的Text@R_634_2419@们中的Text值
for (int i = 0; i < txtCount; i++)
{
//注意:这里必须通过上层容器来获取动态创建的Text@R_634_2419@,才能获取取ViewState内容
span = divControls.FindControl(“spanText@R_634_2419@” + i.ToString()) as HtmlGenericControl ;

if (span != null && span.InnerText.Trim().Length > 0)
{
sbResult.AppendFormat(“Url Address{0}: {1}.<br />”,span.InnerText.Trim());
}
}
divMessage.InnerHtml = sbResult.ToString();
}

希望本文所述对大家的C#程序设计有所帮助。