Gridview Header下拉列表过滤不会触发SelectedIndexChanged方法 [英] Gridview Header dropdown list filtering not firing SelectedIndexChanged method
问题描述
我在gridview_rowdatabound事件中动态填充的gridview的每个列标题中都有下拉列表。它可以用于过滤功能。
我可以得到下拉列表和项目列表,但选择索引更改的方法没有被触发
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
//一些条件
DropDownList ddl = new DropDownList();
//一些ddl属性
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged + = new EventHandler(ddl_OnSelectedIndexChanged);
}
我也放置了一个新的事件处理方法,但是这个事件没有得到被解雇。
private void ddl_OnSelectedIndexChanged(Object sender,EventArgs e)
{
string test =select on索引改变了;
}
有关如何在ddl更改事件上使用此方法的任何建议?
问题是您在<$>内部分配了 SelectedIndexChanged
c $ c> RowDataBound 事件。
因为它只在页面加载时被调用,而不是在PostBack上调用(如果你将数据绑定封装在<
$ <
protected void Page_Load(object sender,EventArgs e )
{
if(!IsPostBack)
{
GridView1.DataSource = mySource;
GridView1.DataBind();
}
DropDownList ddl = GridView1.HeaderRow.FindControl(DropDownList1)as DropDownList;
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged + = new EventHandler(ddl_OnSelectedIndexChanged);
}
I've dropdownlist in the each column header of the gridview that is populated dynamically in gridview_rowdatabound event. It is placed for the filtering functionality.
I'm able to get the drop down and list of items but the select index changed method is not getting fired
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//some condition
DropDownList ddl = new DropDownList();
//some ddl properties
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_OnSelectedIndexChanged);
}
I've also place a new event handler method but this event is not getting fired.
private void ddl_OnSelectedIndexChanged(Object sender, EventArgs e)
{
string test = "reached on select Index changed";
}
Any suggestion on how to get the upto this method on ddl change event ?
The problem is that you assign the SelectedIndexChanged
event inside the RowDataBound
event.
Because it is called only on page load, and not on PostBack (if you wrapped the databinding inside if (!IsPostBack) { }
that is), the binding is lost on PostBack and the method will not be executed.
So you have to make sure you add the handler on every page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = mySource;
GridView1.DataBind();
}
DropDownList ddl = GridView1.HeaderRow.FindControl("DropDownList1") as DropDownList;
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_OnSelectedIndexChanged);
}
这篇关于Gridview Header下拉列表过滤不会触发SelectedIndexChanged方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!