加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在按钮单击时动态添加新文本框

发布时间:2020-12-16 10:01:12 所属栏目:asp.Net 来源:网络整理
导读:我的页面上有文本框“tb1”,我希望用户能够在需要输入更多数据时添加另一个.我有以下代码: VB: ViewState("num") = 2 Dim MyTextBox = New TextBoxMyTextBox.ID = "tb" ViewState("num")MyTextBox.Width = 540MyTextBox.Height = 60MyTextBox.TextMode = T
我的页面上有文本框“tb1”,我希望用户能够在需要输入更多数据时添加另一个.我有以下代码:

VB:

ViewState("num") = 2            
Dim MyTextBox = New TextBox
MyTextBox.ID = "tb" & ViewState("num")
MyTextBox.Width = 540
MyTextBox.Height = 60
MyTextBox.TextMode = TextBoxMode.MultiLine
AddScript.Controls.Add(MyTextBox)
AddScript.Controls.Add(New LiteralControl("<br>"))
ViewState("num") = ViewState("num") + 1

ASP:

<asp:PlaceHolder id="AddScript" runat="server">
    <asp:Label ID="Label2" runat="server" Font-Bold="true" 
        Text="Scripts: (Drag from right)"></asp:Label><br />
    <asp:TextBox ID="tb1" runat="server" Width="90%" Height="60px" 
        TextMode="MultiLine" Enabled="false"></asp:TextBox>
</asp:PlaceHolder>

我的问题是我每次只能添加一个文本框,并且页面上的右侧面板也有一个搜索按钮,如果单击此按钮,则创建的文本框将消失.任何帮助,将不胜感激.

解决方法

您可以构建自己的TextBoxCollection CompositeControl,它将使您能够实现所需的功能.

我已经汇总了一个如何实现它的基本例子.

控制

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

namespace ASPNetWebApplication.Controls
{
    public class TextBoxCollection : CompositeControl
    {
        private List<string> TextBoxes
        {
            get
            {
                if (ViewState["TextBoxes"] == null)
                {
                    ViewState["TextBoxes"] = new List<string>();
                }

                return (List<string>)ViewState["TextBoxes"];
            }
            set
            {
                ViewState["TextBoxes"] = value;
            }
        }

        protected override void CreateChildControls()
        {
            foreach (string textBox in TextBoxes)
            {
                Controls.Add(new TextBox() { ID = textBox });
                Controls.Add(new Literal() { Text = "<br/>" });
            }
        }

        public TextBox GetTextBox(string id)
        {
            return (TextBox)Controls.Cast<Control>().Where(t => (t.ID == null ? "" : t.ID.ToLower()) == id.ToLower()).SingleOrDefault();
        }

        public void AddTextBox(string id)
        {
            TextBoxes.Add(id);
        }
    }
}

示例标记

注意:将Assembly =“ASPNetWebApplication”更改为程序集的名称.

<%@ Register Assembly="ASPNetWebApplication" Namespace="ASPNetWebApplication.Controls" TagPrefix="ASPNetWebApplication" %>

...

<ASPNetWebApplication:TextBoxCollection ID="TextBoxCollection1" runat="server" />
<br />
<asp:Button ID="AddTextBoxesButton" runat="server" OnClick="AddTextBoxesButton_Click" Text="Add Some Text Boxes" />
<asp:Button ID="GetTextBoxValuesButton" runat="server" OnClick="GetTextBoxValuesButton_Click" Text="Get TextBox Values" Visible="false" />
<br />
<asp:Literal ID="TextBoxEntriesLabel" runat="server"></asp:Literal>

示例代码隐藏

protected void AddTextBoxesButton_Click(object sender,EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        TextBoxCollection1.AddTextBox(string.Format("TextBox{0}",i));
    }

    AddTextBoxesButton.Visible = false;
    GetTextBoxValuesButton.Visible = true;
}

protected void GetTextBoxValuesButton_Click(object sender,EventArgs e)
{
    TextBoxEntriesLabel.Text = string.Empty;
    for (int i = 0; i < 10; i++)
    {
        string textBoxId = string.Format("TextBox{0}",i);
        TextBoxEntriesLabel.Text += string.Format("TextBox: {0},Value: {1}<br/>",textBoxId,TextBoxCollection1.GetTextBox(textBoxId).Text);
    }
}

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读