1. 程式人生 > >C#後臺創建控件並獲取值的方法

C#後臺創建控件並獲取值的方法

add border 入門 span num dddd plugin null object

本文實例講述了C#後臺創建控件並獲取值的方法。分享給大家供大家參考。具體實現方法如下:

前臺代碼:


代碼如下:

<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=”獲取控件值” 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時,都需要重新動態創建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#後臺創建控件並獲取值的方法