国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

C#后臺創(chuàng)建控件并獲取值

2019-11-17 01:59:24
字體:
供稿:網(wǎng)友

C#后臺創(chuàng)建控件并獲取值

前臺代碼

 <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" />&nbsp;&nbsp;            <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時,都需要重新動態(tài)創(chuàng)建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++)        {            //創(chuàng)建div             div = new HtmlGenericControl();            div.TagName = "div";            div.ID = "divTextBox" + i.ToString();            div.Attributes["class"] = "item2";            //創(chuàng)建span             span = new HtmlGenericControl();            span.ID = "spanTextBox" + i.ToString();            span.InnerHtml = "Url Address" + (i + 1).ToString() + ":";            //創(chuàng)建TextBox             txt = new TextBox();            txt.ID = "txt" + i.ToString();            txt.CssClass = "input";            //創(chuàng)建格式驗證控件,并且將其關(guān)聯(lián)到對應(yīng)的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- ./?%&amp;=]*)?";            //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);        //遍歷獲取動態(tài)創(chuàng)建的TextBox們中的Text值         for (int i = 0; i < txtCount; i++)        {            //注意:這里必須通過上層容器來獲取動態(tài)創(chuàng)建的TextBox,才能獲取取ViewState內(nèi)容             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());            }        }        //遍歷獲取動態(tài)創(chuàng)建的TextBox們中的Text值         for (int i = 0; i < txtCount; i++)        {            //注意:這里必須通過上層容器來獲取動態(tài)創(chuàng)建的TextBox,才能獲取取ViewState內(nèi)容             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();    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 禹州市| 灵川县| 鄢陵县| 泸州市| 兴安盟| 满洲里市| 辉县市| 修水县| 澄迈县| 崇明县| 榕江县| 晋城| 永安市| 井冈山市| 虞城县| 丽水市| 嘉兴市| 常德市| 兰西县| 新民市| 海口市| 漳浦县| 尤溪县| 长沙县| 台南市| 中卫市| 蒙城县| 湘潭市| 鄯善县| 温宿县| 石狮市| 灵璧县| 康平县| 靖边县| 塔城市| 大姚县| 霍山县| 瑞昌市| 林甸县| 永仁县| 探索|