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?
这样做的基本思想是创建一个动态页面,只需单击一个按钮,我就可以添加尽可能多的控件。控件可以重复。
面板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);
}
答案 0 :(得分:0)
首先让我知道Ph是什么? 相同的代码适合我。我做的是创建了一个名为Ph的面板,然后将CbxList添加到该面板,即Ph
这是代码
*CheckBoxList CbxList = new CheckBoxList();
TextBox txtBox = new TextBox();
RadioButtonList rbList = new RadioButtonList();
rbList.Items.Add("First Radio Button List");
rbList.Items.Add("Second Radio Button List");
rbList.Items.Add("Third Radio Button List");
rbList.Items.Add("Fourth Radio Button List");
rbList.Items.Add("Fifth Radio Button List");
RadioButton rbTest = new RadioButton();
rbTest.Text = "Simple Radio Button";
txtBox.Text = "Simple Text Box";
CbxList.ID = "Cbx";
for (int i = 0; i < intCount; i++)
{
CbxList.Items.Add(new ListItem(Convert.ToChar(i + 65).ToString(), Convert.ToChar(i + 65).ToString()));
}
//Adding controls to Panel
ph.Controls.Add(rbTest);
ph.Controls.Add(CbxList);
ph.Controls.Add(rbList);
ph.Controls.Add(txtBox);
ViewState["ListCreated"] = false;*