动态文本框的问题,如何存储文本值
我有一个问题困扰了我三天。
在我的 Web 表单上,我想要一个按钮,单击该按钮我想在 asp:PlaceHolder 中动态创建五个文本框。
我希望,我在这个 texBoxes 中输入的值即使在回发之后也会被保存。使用第二个按钮我想存储它们。
我读过有关页面生命周期、viewState、IsPostBack 的文章,...很多动态创建控件的文章,但我仍然无法对此进行编程。
我尝试了多种方法,但都没有成功。以下是我的“杰作”的最后一个版本。请帮助我完成这个任务,因为它让我感到恶心。谢谢,马丁
namespace DynamicCreate
{
public partial class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox textBox;
private TextBox[] my_dynamicTextBoxes = new TextBox[5];
private string[] textBoxValues = new string[5];
protected void Page_Load(object sender, System.EventArgs e)
{
btn_save_tb_values.Click += new EventHandler(save_btnClick);
but_load_tb.Click += new EventHandler(creat_tb_btnClick);
int i = 0;
foreach (TextBox tb in my_dynamicTextBoxes)
{
if (ViewState["c_textBox" + i.ToString()] != null)
{
tb.Text = (string)ViewState["c_textBox" + i.ToString()];
i++;
}
else
{
textBox = new TextBox();
textBox.ID = "c_textBox" + i.ToString();
my_dynamicTextBoxes[i] = textBox;
i++;
}
}
}
protected void creat_tb_btnClick(object sender, EventArgs e)
{
int i = 0;
foreach (TextBox neww in my_dynamicTextBoxes)
{
c_placeholder.Controls.Add(neww);
c_placeholder.Controls.Add(new LiteralControl("<br>"));
ViewState["c_textBox" + i.ToString()] = neww.Text;
i++;
}
}
protected void save_btnClick(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++) {}
}
}
}
<form id="form1" runat="server">
<div>
<div> <asp:PlaceHolder ID="c_placeholder" runat="server"></asp:PlaceHolder> </div>
<div> <asp:Button runat="server" ID="but_load_tb" Text="Dodaj Polja!!"/> </div>
<div> <asp:Button runat="server" ID="btn_save_tb_values" Text="Izpisi Vrednosti!" /> </div
</div>
</form>
I have a problem which is bothering me for three days.
On my web form I want to have a button, on which click I want to make dynamicly five text boxes in asp:PlaceHolder.
And I want, that values which I enter in this texBoxes, are saved even after postBack. With second button i want to store them.
I've read articles about lifecycle of page, viewState, IsPostBack,... a lot of articles of dynamically created controls, but I'm still not able to program this.
There are several ways I tried, but without success. Below is the last version of my "masterpiece". Please help me to crate this task, because it makes me sick. Thanks, Martin
namespace DynamicCreate
{
public partial class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox textBox;
private TextBox[] my_dynamicTextBoxes = new TextBox[5];
private string[] textBoxValues = new string[5];
protected void Page_Load(object sender, System.EventArgs e)
{
btn_save_tb_values.Click += new EventHandler(save_btnClick);
but_load_tb.Click += new EventHandler(creat_tb_btnClick);
int i = 0;
foreach (TextBox tb in my_dynamicTextBoxes)
{
if (ViewState["c_textBox" + i.ToString()] != null)
{
tb.Text = (string)ViewState["c_textBox" + i.ToString()];
i++;
}
else
{
textBox = new TextBox();
textBox.ID = "c_textBox" + i.ToString();
my_dynamicTextBoxes[i] = textBox;
i++;
}
}
}
protected void creat_tb_btnClick(object sender, EventArgs e)
{
int i = 0;
foreach (TextBox neww in my_dynamicTextBoxes)
{
c_placeholder.Controls.Add(neww);
c_placeholder.Controls.Add(new LiteralControl("<br>"));
ViewState["c_textBox" + i.ToString()] = neww.Text;
i++;
}
}
protected void save_btnClick(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++) {}
}
}
}
<form id="form1" runat="server">
<div>
<div> <asp:PlaceHolder ID="c_placeholder" runat="server"></asp:PlaceHolder> </div>
<div> <asp:Button runat="server" ID="but_load_tb" Text="Dodaj Polja!!"/> </div>
<div> <asp:Button runat="server" ID="btn_save_tb_values" Text="Izpisi Vrednosti!" /> </div
</div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我想出的一个简单示例,当您单击“创建”按钮时,它将动态地将 5 个文本框添加到占位符,并在您单击“保存”按钮时显示这些值:
Default.aspx:
默认.aspx.cs:
Here's a simple example I came up with that will dynamically add 5 textboxes to a PlaceHolder when you click a "Create" button and will bring up the values when you click a "Save" button:
Default.aspx:
Default.aspx.cs:
这行不通,伙计。
添加需要视图状态等回发信息的控件,在“单击”事件发生时无法添加。只是尝试在点击事件上添加按钮,动态按钮将不起作用。就是这样。你的工作方式必须有点不同。
从头开始创建文本框,将它们添加到占位符中。
将它们设置为隐藏:
mycontrol.visible = false
然后在单击事件中,设置您想要访问它们的尽可能多的“mycontrol”,启用它们,设置它们的值并设置其可见性。当“mycontrol”设置为 onInit 时,您将不会遇到任何问题!
只是一个简单的例子——...
This will not work dude.
Adding Controls, that needs the postback infos like viewstate and so on, cannot be added when the "click" events happend. Just try to add a button on the click event, the dynamcally button will not work. Its just like that. You have to work a little different.
Create your textboxes from beginning, add them to your placeholder.
SET THEM HIDDEN:
mycontrol.visible = false
Then on click event, se as much "mycontrol"s you want to access them, enabling them, setting their values and setting its visibility. When "mycontrol"s are set onInit, you will have no problems!
Just a simple example-...