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?
這樣的基本想法是建立一個動態的頁面,在那裏我可以添加儘可能多的控制,我可以只用點擊一個按鈕。控件可以重複。
Panel pnlTextBox; 保護無效Page_PreInit(對象發件人,EventArgs的){ 在asp.net C#創建動態複選框,單選按鈕
//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);
}
你爲什麼不保證其真實性呢? – Mahdi
我想製作一個完整的動態表單,爲此我需要添加所有控件,一些控件允許我使用真實狀態,有些使用錯誤狀態。 – Faizan