Just juggle some more code using the post back. This time I was using ASP.Net 2.0
I added this code to the Init method
Code:
//Add varying stuff
if (Page.Session.Count == 0)
{
Page.Session.Add("num", 1);
}
else
{
int n = (int)Page.Session["num"];
for (int i = 0; i < n; i++)
{
//Add stuff
TextBox txtBox = new TextBox();
txtBox.Text = "text" + i.ToString();
form.Controls.Add(txtBox);
}
Page.Session["num"] = n + 1;
}
View state was saved automatically without prgramatically accessing the StateBag of the page. I did not test the effects of the PostBack property here. Would like to see any examples where the view state is set programatically.
I tested another code in the Load method after removing the Init method. The Page Load was always called twice. I never realized how much that affects the code until used with dynamic controls. Anyways, the view state was saved automatically. On thing though, controls that are added in the second load call, do not save state. But once the controls are added, they will save state. I also noticed that the nth control always had the same state even when controls are inserted before it and take its state position. This I may have noticed wrong. This was what I was playing with
Code:
protected void Page_Load(object sender, EventArgs e)
{
Control form = Page.FindControl("Form1");//Should find Placeholder
//form.EnableViewState = false;
//Add stuff
Literal lit00 = new Literal();
lit00.Text = "<br><INPUT type=\"submit\" value=\"Submit\"><br>";
form.Controls.Add(lit00);
if (Page.IsPostBack)//(true)//(!Page.IsPostBack)
{
//Add stuff
Literal lit = new Literal();
lit.Text = "<br><INPUT type=\"submit\" value=\"Submit\"><br>";
form.Controls.Add(lit);
if (Page.Session.Count == 0)
{
Page.Session.Add("num", 0);
}
else
{
int n = (int)Page.Session["num"];
Page.Session["num"] = n + 1;
for (int i = 0; i < n; i++)
{
//Add stuff
TextBox txtBox = new TextBox();
//txtBox.EnableViewState = false;
txtBox.Text = sender.ToString() + i.ToString();
//txtBox.EnableViewState = false;
form.Controls.Add(txtBox);
}
}
}
else
{
}
}
View state was saved automatically. I edited various text boxes and clicked submit, change up the code a bit and test again. Sometimes the view state was not saved for freshly added controls in the second call of Load. Perhaps this could have been done programmatically eg ViewState["id"] = "data".
There was a double call on the Load method and I did not consider using the else method of this if to fix the problem
Code:
if (Page.IsPostBack)//(true)//(!Page.IsPostBack)
I could also implement another if to use just the first Load call. I am not sure if the page was bug free or if something else called the Load method explicitly.
@icymint,
I agree with A-->D all that you said. It works. I am a novice to dynamic controls. There is much different implementations that I have not tested. Sometimes you can use the IsPost back and sometimes you do not, but again I am just a novice, so I could be wrong.
To get dynamic controls working, you have to debug a little, but any unforseen bug can throw you off track and that is what happened to us.