Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,
Please see below code:

C#
        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;
}


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.

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

This code not working.
Please help me.
Thanks in advance,
Manu
Posted
Updated 24-Jun-13 20:17pm
v3

1 solution

You will loose all dynamic controls on postback.

You could still store the values of these controls in a session, and then when the page refreshes, recreate these controls and put the values in them.
Ugly, but should work.
 
Share this answer
 
Comments
Manu V Nath 25-Jun-13 2:15am    
Hi Abhinav thanks for the quick reply, I am ready for this ugliness. But the problem is the second point- I am unable to get the text box values. I am tried below code but not working

protected void BtnPublish_click( object sender, EventArgs e ) { TextBox tb = (TextBox)placeHolderTable .FindControl( "TxtNewLabel1" ); }
Abhinav S 25-Jun-13 2:22am    
Try http://www.codeproject.com/Articles/502251/How-to-create-controls-dynamically-in-ASP-NET-and

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900