动态文本框的问题,如何存储文本值

发布于 2024-10-19 02:08:21 字数 2388 浏览 0 评论 0原文

我有一个问题困扰了我三天。

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-10-26 02:08:21

这是我想出的一个简单示例,当您单击“创建”按钮时,它将动态地将 5 个文本框添加到占位符,并在您单击“保存”按钮时显示这些值:

Default.aspx:

<asp:PlaceHolder ID="phButtons" runat="server" />
<asp:Button ID="btnCreate" Text="Create" runat="server" onclick="btnCreate_Click" />
<asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click" />

默认.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (ViewState["buttons"] != null)
        CreateButtons();
}

protected void btnCreate_Click(object sender, EventArgs e)
{
    ViewState["buttons"] = true;
    CreateButtons();
}

protected void btnSave_Click(object sender, EventArgs e)
{
    if (ViewState["buttons"] != null)
    {
        // Save the button information.
        foreach (Control ctl in phButtons.Controls)
        {
            string x;
            if (ctl is TextBox)
                x = (ctl as TextBox).Text;
        }
    }
}

private void CreateButtons()
{
    for (int iLoop = 0; iLoop < 5; iLoop++)
    {
        phButtons.Controls.Add(new TextBox() { ID = "txt" + iLoop });
    }
}

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:

<asp:PlaceHolder ID="phButtons" runat="server" />
<asp:Button ID="btnCreate" Text="Create" runat="server" onclick="btnCreate_Click" />
<asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click" />

Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (ViewState["buttons"] != null)
        CreateButtons();
}

protected void btnCreate_Click(object sender, EventArgs e)
{
    ViewState["buttons"] = true;
    CreateButtons();
}

protected void btnSave_Click(object sender, EventArgs e)
{
    if (ViewState["buttons"] != null)
    {
        // Save the button information.
        foreach (Control ctl in phButtons.Controls)
        {
            string x;
            if (ctl is TextBox)
                x = (ctl as TextBox).Text;
        }
    }
}

private void CreateButtons()
{
    for (int iLoop = 0; iLoop < 5; iLoop++)
    {
        phButtons.Controls.Add(new TextBox() { ID = "txt" + iLoop });
    }
}
大姐,你呐 2024-10-26 02:08:21

这行不通,伙计。

添加需要视图状态等回发信息的控件,在“单击”事件发生时无法添加。只是尝试在点击事件上添加按钮,动态按钮将不起作用。就是这样。你的工作方式必须有点不同。

从头开始创建文本框,将它们添加到占位符中。

将它们设置为隐藏:mycontrol.visible = false

然后在单击事件中,设置您想要访问它们的尽可能多的“mycontrol”,启用它们,设置它们的值并设置其可见性。当“mycontrol”设置为 onInit 时,您将不会遇到任何问题!

private l as new List(of textbox)
protected sub onload()
  for i = 0 to 10
    dim txt as new textbox
    txt.visible = false
    l.add(txt)
    me.controls.add(txt)
  end for
end sub


protected sub onClick()
   dim controlsCountNeeded = 4
   dim q = (from i in me.l where i.visible = false).take(4)
   for each item in q
       item.visible = true
   end for
end sub

只是一个简单的例子——...

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!

private l as new List(of textbox)
protected sub onload()
  for i = 0 to 10
    dim txt as new textbox
    txt.visible = false
    l.add(txt)
    me.controls.add(txt)
  end for
end sub


protected sub onClick()
   dim controlsCountNeeded = 4
   dim q = (from i in me.l where i.visible = false).take(4)
   for each item in q
       item.visible = true
   end for
end sub

Just a simple example-...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文