欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#后台创建控件并获取值的方法

程序员文章站 2023-12-11 20:42:58
本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: