快速业务通道

ASP.NET教程:如何动态写入服务器端控件

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-04-10

说明, 动态创建的TextBox们将装载到divControls中。

3. 后台代码:

以下为引用的内容:

       ///<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- ./?%&amp;=]*)?";
                rev.ErrorMessage = "Invalid url Address!";
 
                //添加控件到容器
                div.Controls.Add(span);
                div.Controls.Add(txt);
                div.Controls.Add(rev);
                divControls.Controls.Add(div);
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                int txtCount = int.Parse(txtTextCount.Text);
 
                // 注意:每次PostBack时,都需要重新动态创建TextBox
                CreateTextBoxList(txtCount);
            }
        }
 
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            txtTextCount.Enabled = false;
            btnCreate.Enabled = false;
        }
 
        protected void btnOK_Click(object sender, EventArgs e)
        {
            TextBox txt;
            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());
                }
            }
 
            divMessage.InnerHtml = sbResult.ToString();
        }

4.       效果图:

ASP.NET教程:如何动态写入服务器端控件-网页教学网

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号