0

我有一个带有下拉列表列的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放在哪里?谢谢

4

1 回答 1

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 的属性。你应该使用任何合适的。这只是一个例子。

更新:

由于网格内的下拉菜单是自动回发的,因此我会将 OnSelectedIndexChanged 的​​事件处理程序添加到网格内的下拉列表中。就像是:

<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.
}
于 2012-05-01T18:15:52.603 回答