开发者

problem with dynamic textBoxes , how to store textValues

开发者 https://www.devze.com 2023-02-12 21:59 出处:网络
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.

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;
               开发者_Go百科 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>


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 });
    }
}


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-...

0

精彩评论

暂无评论...
验证码 换一张
取 消