博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
后台添加控件时,必须每次重画控件,才能从前台获取控件数据。
阅读量:4936 次
发布时间:2019-06-11

本文共 2943 字,大约阅读时间需要 9 分钟。

本文实例讲述了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();
}

转载于:https://www.cnblogs.com/guanshan/p/guan2015-9-22.html

你可能感兴趣的文章
Continuous integration
查看>>
前端知识点总结
查看>>
github 在ubuntu 使用--常用命令
查看>>
hl7 V2中Message Control ID的含义及应用
查看>>
IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)...
查看>>
iOS 修改textholder的颜色
查看>>
【资料】wod地城掉落
查看>>
C# FTPHelper(搬运)
查看>>
C#HttpHelper类1.3正式版教程与升级报告
查看>>
【转】Android 语言切换过程分析
查看>>
jpa 多对多关系的实现注解形式
查看>>
Android开发——View绘制过程源码解析(一)
查看>>
Quartz和TopShelf Windows服务作业调度
查看>>
让ie9之前的版本支持canvas
查看>>
排序规则
查看>>
percent的用法
查看>>
中文词频统计
查看>>
Hibernate三种状态详解
查看>>
判断一个数是否是2^N次方
查看>>
html5特征检测
查看>>