I have a gridview with a dropdownlist column, and i enabled the paging function. The problem is every time after it turns to the next page, the selected value of the dropdownlist on the previous page is back to default value.
I tried to wrap the code with if(!ispostback), only the first page available other pages are disappear
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"];
unverifiedlist.DataSource = employeelist;
unverifiedlist.AllowPaging = true;
unverifiedlist.PageSize = 10;
unverifiedlist.DataBind();
}
}
protected void PageSelect_SelectedIndexChanged(object sender, EventArgs e)
{
int page = int.Parse(PageSelect.SelectedItem.Text);
unverifiedlist.PageIndex = page;
DataBind();
}
<asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled">
<Columns><asp:TemplateField HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server" AutoPostBack="true">
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="false"/>
</asp:GridView>
<asp:DropDownList ID="PageSelect" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList>
does anyone know how to fix it, where should I put ispostback? thanks
You need to handle OnRowDataBound and set the appropriate element programmatically. Example:
And implement something like:
Assuming, obviously, that you have a Property called Unverified on your business object. You should use whatever is appropriate. This is just an example.
UPDATE:
Since the drop down inside the grid is auto posting back, I would add an event handler for OnSelectedIndexChanged to the drop down list inside the Grid. Something like:
And then