2012-05-01 72 views
0

我有一个下拉列表列的gridview,我启用了分页功能。问题在于它每次转到下一页后,上一页上的下拉列表的选定值将恢复为默认值。阅读从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

我希望这个链接将帮助您解决问题。 http://stackoverflow.com/questions/4189158/asp-net-dropdownlist-not-retaining-selected-item-on-postback –

+0

我增加了更多的代码。我在girdview中的下拉列表没有数据源,只有两个值。还有另一个下拉列表来控制gridview分页。当它转到下一页时,页面发回,上一页的下拉列表恢复为默认值。 – pita

回答

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; 

    } 
} 

假设,很显然,你有一个属性叫做未验证您的业务对象。你应该使用适当的东西。这只是一个例子。

UPDATE:

由于下拉网格内是自动回发,我想补充的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. 
} 
+0

@lcarus非常感谢,这是我需要的,但我只是有一个问题,我想下拉列表保持我选择的价值,而不是预填充价值所以,你可以告诉我如何保存选择的价值。谢谢 – pita

+0

您需要将选择基本持久化到绑定到网格的业务对象。我可以想到很多这样做的方法,但它们都是黑客,因为GridView中的下拉列表需要能够告诉你哪个CDPEmployee你正在改变选择,所以你可以从缓存中获取列表 - 这是我看到的地方您存储CDPEmployee的列表并将相应的属性更新为适当的对象。我会发布一个起点的方法。 – Icarus