Basically I am creating a full dynamic form which has text boxes, check boxes, etc.
When I try to add this code in a page where `EnableViewState="false"` it doesn't work but it works fine on a page where `EnableViewState="true"`.
But I want it to work on this (`EnableViewState="false"`) page. How do I do this?
Basic idea of doing this is to create a dynamic page on which I can add as many controls as I can with just clicking one button. Controls can repeat.
Panel pnlTextBox;
protected void Page_PreInit(object sender, EventArgs e)
{
//Create a Dynamic Panel
pnlTextBox = new Panel();
pnlTextBox.ID = "pnlTextBox";
pnlTextBox.BorderWidth = 1;
pnlTextBox.Width = 300;
this.form1.Controls.Add(pnlTextBox);
//Create a LinkDynamic Button to Add TextBoxes
//Recreate Controls
RecreateTextBoxControls("txtDynamic", "TextBox");
RecreateDDLControls("ddlDynamic", "DropDownList");
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// /* int i=Convert.ToInt32(DropDown1.SelectedValue.ToString());
// if (i == 1)
// {*/
int cnt = FindOccurence("txtDynamic");
CreateTextBox("txtDynamic-" + Convert.ToString(cnt + 1));
// /* }
// else if (i == 2)
// {
// int cnt = FindOccurence("ddlDynamic");
// CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt + 1));
// }
// else if (i == 3)
// {
// }
// else if (i == 4)
// {
// }
// else if (i == 5)
// {
// }
// else if (i == 6)
// {
// }
// else
// {
// Console.Write("Bawa ji ka thullu");
// }
// */
}
private int FindOccurence(string substr)
{
string reqstr = Request.Form.ToString();
return ((reqstr.Length - reqstr.Replace(substr, "").Length) / ....substr.Length);
}
private void RecreateTextBoxControls(string ctrlPrefix, string ctrlType)
{
string[] ctrls = Request.Form.ToString().Split('&');
int cnt = FindOccurence(ctrlPrefix);
if (cnt > 0)
{
for (int k = 1; k <= cnt; k++)
{
for (int i = 0; i < ctrls.Length; i++)
{
if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &&
!ctrls[i].Contains("EVENTTARGET"))
{
string ctrlID = ctrls[i].Split('=')[0];
if (ctrlType == "TextBox")
{
CreateTextBox(ctrlID);
}
break;
}
}
}
}
}
private void RecreateDDLControls(string ctrlPrefix, string ctrlType)
{
string[] ctrls = Request.Form.ToString().Split('&');
int cnt = FindOccurence(ctrlPrefix);
if (cnt > 0)
{
for (int k = 1; k <= cnt; k++)
{
for (int i = 0; i < ctrls.Length; i++)
{
if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &&
!ctrls[i].Contains("EVENTTARGET"))
{
string ctrlID = ctrls[i].Split('=')[0];
if (ctrlType == "DropDownList")
{
CreateDropDownList(ctrlID);
}
break;
}
}
}
}
}
private void CreateDropDownList(string ID)
{
DropDownList ddl = new DropDownList();
ddl.ID = ID;
ddl.Items.Add(new ListItem("--Select--", ""));
ddl.Items.Add(new ListItem("One", "1"));
ddl.Items.Add(new ListItem("Two", "2"));
ddl.Items.Add(new ListItem("Three", "3"));
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
pnlTextBox.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBox.Controls.Add(lt);
}
private void CreateTextBox(string ID)
{
TextBox txt = new TextBox();
txt.ID = ID;
txt.AutoPostBack = true;
txt.TextChanged += new EventHandler(OnTextChanged);
pnlTextBox.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBox.Controls.Add(lt);
}