-1

I created a webform that allows the user to dynamically add more textboxes. Right now, as the button is clicked to add another field, it postbacks to itself. The controls are added in the Pre_Init. However when the page reconstructs itself the textbox names are different each time so the data is not being retained on each postback.

protected void Page_PreInit(object sender, EventArgs e)
{

    MasterPage master = this.Master; //had to do this so that controls could be added. 
    createNewTextField("initEnumValue",true);
    RecreateControls("enumValue", "newRow");
}
private int FindOccurence(string substr)
{
    string reqstr = Request.Form.ToString();

    return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
}
private void RecreateControls(string ctrlPrefix, string ctrlType)
{
    string[] ctrls = Request.Form.ToString().Split('&');
    int cnt = FindOccurence(ctrlPrefix);
    //Response.Write(cnt.ToString() + "<br>");
    if (cnt > 0)
    {
        for (int k = 1; k <= cnt; k++)
        {
            for (int i = 0; i < ctrls.Length; i++)
            {
                if (ctrls[i].Contains(ctrlPrefix  + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                {
                    string ctrlID = ctrls[i].Split('=')[0];

                    if (ctrlType == "newRow")
                    {

                        createNewTextField(ctrlID,false);
                    }
                    break;
                }
            }
        }
    }
}
protected void addEnum_Click(object sender, ImageClickEventArgs e)
{

    int cnt = FindOccurence("enumValue");
    createNewTextField("enumValue" + Convert.ToString(cnt + 1),false);
   // Response.Write(cnt.ToString() + "<br>");
}
private void createNewTextField(string ID, bool button)
{
    Response.Write(ID + "<br/>"); //this is where I'm getting different names each time there is a postback
    if (ID != "initEnumValue") //create new line starting with the second tb. 
    {
        LiteralControl newLine = new LiteralControl();
        newLine.Text = "<br />";
        this.plhEnum.Controls.Add(newLine);
    }



    TextBox newTb = new TextBox();
    newTb.ID = ID;
    this.plhEnum.Controls.Add(newTb);

    if (button) //create the button only on the first one. 
    {
        LiteralControl space = new LiteralControl();
        space.Text = "&nbsp;";
        this.plhEnum.Controls.Add(space);
        ImageButton imgbutton = new ImageButton();
        imgbutton.ID = "addEnum";
        imgbutton.ImageUrl = "~/images/add.png";
        imgbutton.Click += new ImageClickEventHandler(addEnum_Click);
        imgbutton.CausesValidation = false;

        this.plhEnum.Controls.Add(imgbutton);
    }

    //endEnumRow();
    //this.plhEnum.Controls.Add(newTb);
}

I have tried this solution How can I get data from dynamic generated controls in ASP .NET MVC?

Community
  • 1
  • 1
Dan
  • 1,041
  • 1
  • 12
  • 32
  • Can you keep the `Textbox` control in the `ViewState` once it is created ? so next time during `PostBack` you need not to create it and just add it to the panel under which you may be adding by accessing the `ViewState`. – Pankaj Apr 12 '12 at 16:11
  • See I don't know how to keep it in the viewstate. I've tried enabling it via .EnableViewState=true; – Dan Apr 13 '12 at 00:28

0 Answers0