asp.net-动态创建的下拉列表不在回发中调用事件处理程序方法
我有一个页面要做一个Hierarchical搜索,它从一个下拉列表开始,根据下拉列表中选择的值,它将查询数据库并在另一个下拉列表中显示孩子们,只要它点击叶子,这个过程就会继续。。。因此,我首先动态添加了下拉列表,它更改了SelectedIndex上的事件处理程序,当我更改所选值时,它会触发回发,但不会调用事件处理程序方法。。不知道我做错了什么。。还是一只虫子 使用会话变量跟踪创建的控件asp.net-动态创建的下拉列表不在回发中调用事件处理程序方法,asp.net,dynamic,drop-down-menu,Asp.net,Dynamic,Drop Down Menu,我有一个页面要做一个Hierarchical搜索,它从一个下拉列表开始,根据下拉列表中选择的值,它将查询数据库并在另一个下拉列表中显示孩子们,只要它点击叶子,这个过程就会继续。。。因此,我首先动态添加了下拉列表,它更改了SelectedIndex上的事件处理程序,当我更改所选值时,它会触发回发,但不会调用事件处理程序方法。。不知道我做错了什么。。还是一只虫子 使用会话变量跟踪创建的控件 private List<DynamicControlProperties> PersistedC
private List<DynamicControlProperties> PersistedControls
{
get
{
if (_persistedControls == null)
{
if (Session[PersistedControlsKey] == null)
{
Session[PersistedControlsKey] = new List<DynamicControlProperties>();
}
_persistedControls = Session[PersistedControlsKey] as List<DynamicControlProperties>;
}
return _persistedControls;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
// regenerate the persisted controls
foreach (var prop in PersistedControls)
{
CreateControl(prop);
}
}
在页面加载中,创建了第一个下拉列表
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// create the control
CreateControl(....)
// bind the data to the dropdown
}
}
在createcontrol方法中,只需创建一个标签和一个下拉列表,将其包装在一个容器中,并将其添加到placeholder中
private DropDownList CreateControl(DynamicControlProperties dynamiccntrlprop)
{
// create a new HTML row
HtmlGenericControlWithParentID tr = new HtmlGenericControlWithParentID("tr");
HtmlGenericControlWithParentID td1 = new HtmlGenericControlWithParentID("td");
HtmlGenericControlWithParentID td2 = new HtmlGenericControlWithParentID("td");
// make sure we set the id and parentid
tr.ID = string.Format("tr{0}", dynamiccntrlprop.ID);
tr.ParentID = dynamiccntrlprop.ParentID;
tr.EnableViewState = true;
// create a new label for dropdown
Label lbl = new Label() { ID = string.Format("lbl{0}", dynamiccntrlprop.DisplayName), Text = dynamiccntrlprop.DisplayName };
// create a new dropdown list
DropDownList ddl = new DropDownList()
{
ID = string.Format("ddl{0}", dynamiccntrlprop.DisplayName),
// set the postback
AutoPostBack = true,
EnableViewState = true
};
// subscribe for the select index changed event
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
// add the controls to table row
td1.Controls.Add(lbl);
td2.Controls.Add(ddl);
tr.Controls.Add(td1);
tr.Controls.Add(td2);
// add the control to place holder
this.filtersPlaceHolder.Controls.Add(tr);
return ddl;
}
这是索引更改处理程序
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
}
启用了视图状态,自动回邮等等。。。在回发中重新创建了具有相同id的控件。。在谷歌上尝试了所有答案。。但是没有运气。。当我更改索引但不调用事件处理程序方法时,它确实会触发回发
有什么想法吗
非常感谢,,
K您必须确保在每个页面回发时都调用CreateControl方法。这需要确保在回发后连接动态控件的事件处理程序
protected void Page_Load(object sender, EventArgs e)
{
// you shouldn't wrap the call to CreateControl in this 'if' statement
//if (!Page.IsPostBack)
//{
// create the control
CreateControl(....)
// bind the data to the dropdown
//}
}
执行此操作后,将触发所选索引更改事件。可能是因为没有加载dropdownlist的新值。 受保护的覆盖无效LoadViewState(对象保存状态) {
// regenerate the persisted controls
foreach (var prop in PersistedControls)
{
CreateControl(prop);
}
base.LoadViewState(savedState);
}