Click here to Skip to main content
15,610,088 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a HTML table,some textboxes and a submit button. i am creating HTML table and textboxes at run i need these textboxes value on submit button.
but when i check in my c# code, they show null or "". and when my page do post back, my table and textboxes also remove automatically.
i try to use Ajax script manager and update panel control also to stop full post back.

Now how can i get my dynamic control after post back and how i get values in c#.???

Thanks in advance.
Telstra 29-Apr-14 1:26am    
When your page is post back then you dynamically crated controls sure will be lost because you are not maintaining it's view state after post back. This problem is in code. You can not solve it using ajax controls.
Please post your code here I will modify it.
204.sharma 29-Apr-14 1:41am    
Here is my code to create dynamic controls.

HtmlTable tableContent = new HtmlTable();
HtmlTableRow row = null;
HtmlTableCell cell = null;
datalayer dl = new datalayer();
DataTable dt = new DataTable();
TextBox txt_desc;

//load parameters

tableContent.ID = "parameterTable";
dt = dl.SelectDescriptors(TreeView2.SelectedNode.Value.ToString().Replace("doctype",""));

//setting table properties
tableContent.Width = "100%";
tableContent.Style.Add("margin", "0");
if (dt.Rows.Count > 0)
foreach (Control ctrl in Page.Controls)
if (Convert.ToString(ctrl.ID) == "parameterTable")

for (int i = 0; i < dt.Rows.Count; i++)

row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = dt.Rows[i]["descriptor_name"].ToString().ToUpper();

txt_desc = new TextBox();
txt_desc.ID = "txt_" + dt.Rows[i]["descriptor_name"].ToString();

cell = new HtmlTableCell();



and this is the code to get controls values

<pre lang="xml">TextBox <pre lang="cs">txtdesc = new TextBox();
txtdesc.ID = &quot;txt_&quot; + dt.Rows[i][&quot;descriptor_name&quot;].ToString();
if (txtdesc.Text != &quot;&quot;)
//emptyTextbox = false;
returnValue = returnValue + &quot; and &quot; + dt.Rows[i][&quot;descriptor_name&quot;].ToString() + &quot; = &#39;&quot; + txtdesc.Text + &quot;&#39;&quot;;
204.sharma 29-Apr-14 1:42am    
Here is the code to get values

txtdesc = new TextBox();
txtdesc.ID = "txt_" + dt.Rows[i]["descriptor_name"].ToString();
if (txtdesc.Text != "")
//emptyTextbox = false;
returnValue = returnValue + " and " + dt.Rows[i]["descriptor_name"].ToString() + " = '" + txtdesc.Text + "'";
Sergey Alexandrovich Kryukov 29-Apr-14 1:31am    
Keep "checking in your C# code", because you did not gave us a chance to check anything. :-)

1 solution

Just understand the concept of dynamic html table. Then you can do it
Problem Reason:
To elaborate on the above statement, the page is recreated each time it is posted back to the server. In other words, a new instance of the Page class is created and the class variables are set using the values from the ViewState. However during this recreation, the dynamically created controls are no longer available and hence the values are lost in the viewstate.
That's why it's showing null on submit (on postback time).

Simply You are missing one thing.... That is you need to define this code or method inside the Page_Init() or Page_Load().To override this behavior, you need to somehow make these controls available on each postback.
Like. Ex.
protected void Page_Load(object sender, EventArgs e)
        // Run only once a postback has occured
        if (Page.IsPostBack)
            //entered by the user in the respective textboxes
          //Don't call your dynamic method here
           //don't write here dynamic control creation code
        //Call outside of postback
Share this answer

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