我有一个带有下拉列表列的gridview,我启用了分页功能。问题是每次转到下一页后,上一页下拉列表的选定值都恢复为默认值。
我尝试用if(!ispostback)
包装代码,只有第一页可用其他页面消失
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>
有谁知道如何修复它,我应该把ispostback放在哪里?谢谢
答案 0 :(得分:1)
您需要处理OnRowDataBound并以编程方式设置相应的元素。例如:
<asp:GridView ID="unverifiedlist" runat="server"
OnRowDataBound="unverifiedlist_RowDataBound" AutoGenerateColumns="false"
AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled">
实现类似:
protected void unverifiedlist_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
((DropDownList)e.Row.FindControl("options")).SelectedValue=((CPDEmployee)e.Row.DataItem).Unverified;
}
}
显然,假设您的业务对象上有一个名为Unverified的Property。你应该使用任何合适的东西。这只是一个例子。
<强>更新强>
由于网格内的下拉是自动回发的,我会将OnSelectedIndexChanged的事件处理程序添加到Grid内的下拉列表中。类似的东西:
<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged">
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>
然后
protected void options_SelectedIndexChanged(object sender, EventArgs e)
{
string selecteValue = ((DropDownList)sender).SelectedValue;
//Now persist this value in the appropriate business object
//this is the difficult part because you don't know for which row in the gridview
//you are changing this selection. You'll need to devise a way to pass an extra
//value (an Employee ID, I would imagine) that would allow you to grab the
// record from the List<CDPEmployee> and change the property to the new selection.
}