动态添加按钮点击一个新的文本框 [英] Dynamically add a new text box on button click
问题描述
我有我的网页上的文本框TB1,我希望用户能够添加另一个,如果他们需要输入更多的数据。我有以下的code:
I have textbox "tb1" on my page and I want users to be able to add another if they need to input more data. I have the following code:
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>
我的问题是,我可以每次只添加一个文本框,我也有在页面右侧面板中的搜索按钮,如果单击此按钮创建文本框就会消失。任何帮助将是AP preciated。
My problem is that I can only add one text box each time and I also have a search button for the right panel on the page and if this button is clicked the created textbox will disappear. Any help would be appreciated.
推荐答案
您可以创建自己的TextBoxCollection的CompositeControl这将使你实现你需要的功能。
You could build your own TextBoxCollection CompositeControl which would enable you to achieve the functionality you require.
我已经把它如何实现一个基本的例子。
I have put together a basic example of how it could be achieved.
控制
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);
}
}
}
示例标记
注意:更改大会=ASPNetWebApplication
你的程序集的名称
Note: Change Assembly="ASPNetWebApplication"
to the name of your assembly.
<%@ 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>
示例codebehind
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);
}
}
希望这有助于。
这篇关于动态添加按钮点击一个新的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!