`
haimingyoung
  • 浏览: 39337 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Asp.Net动态添加文本框,并获得文本框的值

    博客分类:
  • .net
 
阅读更多

前台aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="SurveyOnline.test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .item
        {
            margin: 10px;
            border-bottom: solid 1px #CCC;
        }
        
        .item2
        {
            margin: 5px;
        }
        
        .input
        {
            width: 200px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div class="item">
            Please input a number:
            <asp:TextBox runat="server" CssClass="item" ID="txtTextCount"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTextCount"
                ValidationGroup="CreateTextBox" Display="Dynamic" ErrorMessage="Required to input content!"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtTextCount"
                ValidationGroup="CreateTextBox" Display="Dynamic" runat="server" ErrorMessage="Only number is valid!"
                ValidationExpression="^\d+$"></asp:RegularExpressionValidator>&nbsp;&nbsp;
            <asp:Button runat="server" ID="btnCreate" Text="Create TextBox List" ValidationGroup="CreateTextBox"
                OnClick="btnCreate_Click" />&nbsp;&nbsp;
            <asp:Button runat="server" ID="btnOK" Text="Get TextBox Content" ValidationGroup="ShowListContent"
                OnClick="btnOK_Click" />
        </div>
        <div runat="server" id="divControls" class="item">
        </div>
        <div runat="server" id="divMessage">
        </div>
    </div>
    </form>
</body>
</html>

 后天代码cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace SurveyOnline
{
    public partial class test : System.Web.UI.Page
    {
        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();
        }
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics