【问题标题】:Read dropdownlist value from a gridview从网格视图中读取下拉列表值
【发布时间】:2012-05-01 17:35:59
【问题描述】:

我有一个带有下拉列表列的网格视图,并且我启用了分页功能。问题是每次翻到下一页后,上一页下拉列表的选中值又回到默认值。

我尝试用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 放在哪里?谢谢

【问题讨论】:

  • 希望此链接能帮助您解决问题。 stackoverflow.com/questions/4189158/…
  • 我添加了更多代码。我在 girdview 中的下拉列表没有数据源,只有两个值。还有另一个下拉列表来控制 gridview 分页。当它转到下一页时,页面回发,上一页的下拉列表恢复为默认值。

标签: asp.net


【解决方案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.
}

【讨论】:

  • @lcarus 非常感谢,这正是我所需要的,但我还有一个问题,我希望下拉列表保留我选择的值,而不是预先填充的值所以你能告诉我如何保存选择的值。谢谢
  • 您需要将选择保留到您绑定到网格的业务对象。我可以想到很多方法来做到这一点,但它们都是黑客,因为你的 gridview 中的下拉列表需要能够告诉你你正在为哪个 CDPEmployee 更改选择,这样你就可以从缓存中获取列表 - 这就是我看到的地方您存储 CDPEmployee- 列表并将适当的属性更新为适当的对象。我将发布一个起点方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2015-02-15
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多