C#後台建立控制項並擷取值的方法

來源:互聯網
上載者:User

標籤:sage   orm   post   evel   exp   led   i++   load   name   

本文執行個體講述了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#後台建立控制項並擷取值的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.