A.NET教程:如何动态写入服务器端控件.(2)[图]_.NET概论及软件使用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

A.NET教程:如何动态写入服务器端控件.(2)[图]

作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0

本篇关键词:服务器控件动态教程
黑客防线网安网讯:    . 后台代码:以下为引用的内容:       ///<summary>         /// Create textbox list        ///</summary>        ///<param name="num">textbox list count</param>        privat...

    . 后台代码:

以下为引用的内容:

       ///<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.       效果图:


    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14115-1.html
网站维护教程更新时间:2012-03-30 05:26:31  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer