程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#後台創立控件並獲得值的辦法

C#後台創立控件並獲得值的辦法

編輯:C#入門知識

C#後台創立控件並獲得值的辦法。本站提示廣大學習愛好者:(C#後台創立控件並獲得值的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#後台創立控件並獲得值的辦法正文


本文實例講述了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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved