我有一个Gridview,必须在Pager行的运行时添加Dropdown列表。我在Gridview RowCreated上添加了以下代码。
protected void gv_transaction_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
using (DropDownList ddlpagesize = new DropDownList())
{
ddlpagesize.Items.Add("25");
ddlpagesize.Items.Add("50");
ddlpagesize.Items.Add("75");
ddlpagesize.Items.Add("100");
ddlpagesize.Items.Add("150");
ddlpagesize.Items.Add("200");
ddlpagesize.AutoPostBack = true;
ddlpagesize.Items.FindByText(gv_transaction.PageSize.ToString()).Selected = true;
ddlpagesize.SelectedIndexChanged += ddlpagesize_SelectedIndexChanged;
using (Table tbl = (Table)e.Row.Cells[0].Controls[0])
{
using (TableCell cell = new TableCell())
{
cell.Controls.Add(new LiteralControl("<b>Page Size: </b>"));
cell.Controls.Add(ddlpagesize);
tbl.Rows[0].Cells.AddAt(0, cell);
}
}
}
}
}
protected void ddlpagesize_SelectedIndexChanged(object sender, EventArgs e)
{
using (DropDownList ddlpagesize = (DropDownList)sender)
{
gv_transaction.PageSize = int.Parse(ddlpagesize.SelectedValue);
gv_transaction.PageIndex = 0;
BindTransactionGrid();
}
}
现在,当我更改下拉列表值时,SelectedIndex更改事件未触发。 但有趣的是,当我从页面大小Dropdownlist的启动中删除using语句时; Selectedindex活动完美无缺。请告诉我是否与处理Gridview动态下拉列表中的dropdownlist和selectedIndex Changed事件有任何关系
答案 0 :(得分:0)
你不需要在using语句中包装asp.net控件,asp.net会在你的控件上自动调用dispose,我认为你的using语句会导致它们过早处理。