1
我在页面顶部有两个按钮添加控件和清除控件。维护包含控件的面板列表。丢失事件处理程序
单击添加控件触发一个函数,该函数创建一个新面板和一个新的DropDownList。
我为DropDownList指定了SelectedIndexChanged的Eventhandler。
这个想法是,根据我的DropDownList中的选择,我将在该面板中创建另一个控件。
最终面板将包含一些可以作为查询参数传递的字段。
我将面板存储在每次回发时重新创建的面板列表中。
我的问题是我可以添加面板,删除面板,并清除我的列表中的所有面板,但我似乎无法得到SelectedIndexChanged触发器触发......它似乎永远不会被分配或它在每个Postback中丢失。
我已经耗尽了我的谷歌搜索专业知识,我对自己感到非常沮丧。我接受建议/修复。
谢谢。
List<Panel> persistControls = new List<Panel>();
protected override void OnInit(EventArgs e)
{
if (Session["persistControls"] != null)
{
persistControls = (List<Panel>)Session["persistControls"];
int count = 0;
foreach (Panel dynamicControl in persistControls)
{
AddQuestionTypeDropDownList(count);
dynamicControl.ID = "panel" + count;
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = count.ToString();
// Pushing to Placeholder
myPlaceholder.Controls.Add(dynamicControl);
myPlaceholder.Controls.Add(btnRemove);
count++;
}
}
base.OnInit(e);
}
// Calls three functions responsible for pulling from the Database and binding the Datagrid.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetClustersFromDatabase(userid);
BindGrid();
BindState();
}
}
private DropDownList AddQuestionTypeDropDownList(int count)
{
DropDownList list = new DropDownList();
list.ID = "list" + count;
list.Items.Add(new ListItem("--Select One--", ""));
list.Items.Add(new ListItem("Title", "1"));
list.Items.Add(new ListItem("Contact", "2"));
list.Items.Add(new ListItem("Date Created", "3"));
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
return list;
}
private DropDownList AddQuestionTypeDropDownList()
{
DropDownList list = new DropDownList();
list.Items.Add(new ListItem("--Select One--", ""));
list.Items.Add(new ListItem("Title", "1"));
list.Items.Add(new ListItem("Contact", "2"));
list.Items.Add(new ListItem("Date Created", "3"));
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
return list;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
Panel panelContainer = new Panel();
panelContainer.ID = "panel" + persistControls.Count;
panelContainer.Controls.Add(AddQuestionTypeDropDownList());
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = persistControls.Count.ToString();
myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page.
persistControls.Add(panelContainer);// Adds our Panel to the Control list
myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page.
Session["persistControls"] = persistControls; // put it in the session
}
catch
{
throw;
}
}
private void list_SelectedIndexChanged(object sender, EventArgs e)
{
//I have no code here but thats because I currently can't even get it to fire.
try
{
}
catch
{
throw;
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
try
{
Session["persistControls"] = null;
Response.Redirect(Request.Url.ToString());
}
catch
{
throw;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
int deleteThisOne = int.Parse(((Button)sender).CommandArgument);
persistControls.Remove(persistControls[deleteThisOne]);
Session["persistControls"] = persistControls;
Response.Redirect(Request.Url.ToString());
}
catch
{
throw;
}
}
您应该将事件添加到Page_Load中的控件中。在OnInit中注册的事件绝不会触发...... – 2012-03-01 18:40:13