Dynamically adding Page size dropdown list in the Gridview Pager
I have a Gridview for which Dropdown list has to be added on the run time at the Pager row. I have added the below code on the 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();
}
}
Now, SelectedIndex change event is not firing, when I change the dropdownlist value. But interestingly, when I remove the using statement from the initiation of page size Dropdownlist; Selectedindex event is firing perfectly. Please tell me if there is any relation with the disposing of dropdownlist and selectedIndex Changed event for the dynamic dropdown in a Gridview
You don't need to wrap asp.net controls in using statements, asp.net will call dispose automatically on your controls, i think your using statements are causing them to be disposed too early.
精彩评论