在Gridview Pager中动态添加页面大小下拉列表
本文关键字:下拉列表 添加 Gridview Pager 动态 | 更新日期: 2023-09-27 17:59:28
我有一个Gridview,必须在Pager行的运行时为其添加下拉列表。我已经在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();
}
}
现在,当我更改dropdownlist值时,SelectedIndex更改事件不会触发。但有趣的是,当我从页面大小的Dropdownlist的初始化中删除using语句时;所选的索引事件正在完美启动。请告诉我,对于Gridview 中的动态下拉列表,丢弃dropdownlist和selectedIndex Changed事件是否有任何关系
您不需要在using语句中包装asp.net控件,asp.net会自动对您的控件调用dispose,我认为您的using语句导致它们过早地被处理。