C#後臺創建控件並獲取值的方法
本文實例講述了C#後臺創建控件並獲取值的方法。分享給大家供大家參考。具體實現方法如下:
前臺代碼:
代碼如下:
<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=”獲取控件值” ValidationGroup=”ShowListContent”
OnClick=”btnOK_Click” />
</div>
<div runat=”server” id=”divControls” class=”item”>
</div>
<div runat=”server” id=”divMessage”>
</div>
</div>
</form>
後臺代碼:
代碼如下:
{
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();
}
C#後臺創建控件並獲取值的方法
本文地址: http://www.paobuke.com/develop/c-develop/pbk23156.html
相關內容
C#實現十五子遊戲詳解MongoDB for C#基礎入門C#中Json字符串的各種應用類實例講解C#多線程處理多個隊列數據的方法C# MVC模式中應該怎樣區分應用程序邏輯(Controller層)和業務邏輯(Model層)?C#中結構(struct)的部分初始化和完全初始化實例分析C#觀察者模式(Observer Pattern)實例教程C#中類與結構的區別實例分析
C#後臺創建控件並獲取值的方法