Manu
Manu

Reputation: 89

Lost dynamically created text box values

Please see below code:

    protected void btnAddField_click( Object sender, EventArgs e ) {
        int FieldCount = 0;
        if (ViewState["FieldCount"] != null)
        {
            FieldCount = (int)ViewState["FieldCount"];
        }

        Table tbl = new Table();
        if (Session["DynamicTable"] != null)
        {
            tbl = (Table)Session["DynamicTable"];
        }

        CheckBox chkNewField = new CheckBox();
        chkNewField.ID = "chkNewField" + FieldCount.ToString();
        chkNewField.Checked = true;

        Label LblNewLabel = new Label();
        LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
        LblNewLabel.Text = "New Lable";

        TextBox TxtNewLabel = new TextBox();
        TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

        Label LblNewValue = new Label();
        LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
        LblNewValue.Text = "New Value";

        TextBox TxtNewValue = new TextBox();
        TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

        TableRow tRow = new TableRow();

        TableCell tCell1 = new TableCell();
        TableCell tCell2 = new TableCell();
        tCell2.Attributes.Add("class", "medium");
        TableCell tCell3 = new TableCell();
        tCell3.Attributes.Add("class", "medium");
        TableCell tCell4 = new TableCell();
        TableCell tCell5 = new TableCell();
        tCell5.Attributes.Add("class", "medium");
        TableCell tCell6 = new TableCell();
        tCell6.Attributes.Add("class", "medium");

        tCell1.Controls.Add(chkNewField);
        tCell2.Controls.Add(LblNewLabel);
        tCell3.Controls.Add(TxtNewLabel);
        tCell4.Controls.Add(new LiteralControl(""));
        tCell5.Controls.Add(LblNewValue);
        tCell6.Controls.Add(TxtNewValue);

        tRow.Cells.Add(tCell1);
        tRow.Cells.Add(tCell2);
        tRow.Cells.Add(tCell3);
        tRow.Cells.Add(tCell4);
        tRow.Cells.Add(tCell5);
        tRow.Cells.Add(tCell6);

        tbl.Rows.Add(tRow);
        placeHolderTable.Controls.Remove(tbl);
        placeHolderTable.Controls.Add(tbl);
        Session["DynamicTable"] = tbl;
        FieldCount++;
        ViewState["FieldCount"] = FieldCount;
}

protected void BtnPublish_click( object sender, EventArgs e ) { 
   TextBox tb = (TextBox)placeHolderTable .FindControl( "TxtNewLabel1" ); 
}

Dynamically adding fields are working fine. But

  1. the values i entered in text boxes clearing on each post back
  2. I failed to fetch values from text boxes.

Please help me. Thanks in advance, Manu

Upvotes: 0

Views: 1059

Answers (2)

Debajit Mukhopadhyay
Debajit Mukhopadhyay

Reputation: 4172

Although there is a tricky way by which you can get the values of the textBoxes in PostBack. You can use this Code below.

private string GetValue(string ControlID)
{
     string[] keys = Request.Form.AllKeys;
     string value = string.Empty;
     foreach (string key in keys)
     {
         if (key.IndexOf(ControlID) >= 0)
         {
             value = Request.Form[key].ToString();
             break;
         }
     }

     return value;
}

Then use this method in PostBack.

protected void BtnPublish_click( object sender, EventArgs e ) 
{ 
    string TxtNewLabel1Val = GetValue("TxtNewLabel1");
}

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

The simple rule about dynamically created controls is that you have to recreate them on Init if you want to receive any values.

Refer: Page life cycle

Postback data is processed in the function ProcessPostData if your controls are not created before that, they user inputs will not get assigned.

I would put the control creation into a method, track that dynamic controls are added in the session(or ViewState), and add them on every Init after that till I want the controls on the page.

Upvotes: 1

Related Questions