Question

I am adding the dynamically TextBox in the placeholder on the button click in that.

When all the textboxes are loaded I am making changes in the values and again Press another Button to save the values to the SharePoint List. But when I press the Save button and I checked the placeholder by debugging it I found that there were no any control in the placeholder.
I am adding the Controls like follows :

TextBox[] tb = new TextBox[item_ans.Count];
Literal[] lt = new Literal[item_ans.Count];
for (int j = 0; j < item_ans.Count; j++)
{
   ans_id.Add(item_ans[j]["ID"].ToString());
   tb[j] = new TextBox();
   tb[j].ID = "tb_ans" + (j + 1).ToString();
   tb[j].Text = item_ans[j]["Title"].ToString();
   lt[j] = new Literal();
   lt[j].Text = "<br/>";
   pl_hd_ans.Controls.Add(tb[j]);
   pl_hd_ans.Controls.Add(lt[j]);
}

And on the Save Button click I am Retrieving those TextBoxes Like follows:

int n = Convert.ToInt32(ViewState["totalAns"].ToString());

foreach (var i in ans_id)
{
    var item_ans = list_ans.GetItemById(i);
    clientContext.Load(item_ans);
    clientContext.ExecuteQuery();

    for (int k = 0; k < n; k++)
    {
       TextBox tb = (TextBox)pl_hd_ans.FindControl("tb_ans" + (k + 1).ToString());
       item_ans["Title"] = tb.Text;
       item_ans.Update();
       clientContext.Load(item_ans);
       clientContext.ExecuteQuery();
    }                
}

But in this I check the Placeholder's Controls that were 0. Can Any one please help me to solve this..?

Was it helpful?

Solution

I'm assuming its ASP.NET WebForms what we're talking about here.

When you are adding controls dynamically to the webpage, you have to recreate them on each sequential postback. The reason for this, is that the dynamically created controls are not present in the .aspx file, nor in the viewstate, so asp.net cannot know that it has to recreate these controls. Therefore, you yourself have to recreate them in the initialized-event (before the page-load), including adding any event handlers that you need.

You can google about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top