【问题标题】:How to retain multiple dropdown list's selected value even when page index is changing?即使页面索引发生变化,如何保留多个下拉列表的选定值?
【发布时间】:2014-03-19 11:01:51
【问题描述】:

我是asp.net 开发的新手。任何帮助将非常感激。 :) 我需要完成的是在我的网格视图中保留我的下拉列表的选定值,即使用户导航到下一页索引也是如此。我正在考虑在页面更改时将值放入会话中,并在页面再次显示时将其放回。我只尝试在复选框中执行此操作,但我不知道如何使用下拉列表来实现。顺便说一句,我有 4 个下拉列表。请帮忙。非常感谢。下面是我的代码

 <asp:GridView ID="gvwAssociation" runat="server" AutoGenerateColumns="False" 
                            AllowSorting="True" HorizontalAlign="Left" AllowPaging="true" Height="75%" Width="100%" SkinID="TitleReviewGridViewSkin" 
                           OnRowDataBound="gvwAssociation_RowDataBound" PageSize="20" OnPageIndexChanging="gvwAssociation_PageIndexChanging" DataKeyNames="ID">
                            <Columns>
                                <asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
                                <asp:BoundField DataField="ID" HeaderText="ID" Visible="true"/>
                                <asp:BoundField DataField="_fileName" HeaderText="File Name"/>
                                <asp:BoundField DataField="_uploadDate" HeaderText="Upload Date" DataFormatString="{0:MM-dd-yyyy}"  />

                                 <asp:TemplateField HeaderText="Pool">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemTemplate>
                                        <%--<asp:dropdownlist ID="ddlPool" runat="server"  ReadOnly="false" Width="75px" ForeColor="Black" MaxLength="6" EnableViewState="true"></asp:dropdownlist>                                      --%>
                                        <asp:DropDownList ID="ddlpool" width="75px" runat="server" AutoPostBack="false"></asp:DropDownList>                                           
                                         </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Year">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemTemplate>
                                        <%--<asp:dropdownlist ID="ddlyear" runat="server" ReadOnly="false" Width="75px" ForeColor="Black" MaxLength="6" EnableViewState="true"></asp:dropdownlist>--%>                                      
                                        <asp:DropDownList ID="ddlyear" width="75px" runat="server" AutoPostBack="false"></asp:DropDownList>                                         
                                         </ItemTemplate>
                                </asp:TemplateField>

                                 <asp:TemplateField HeaderText="Plant">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemTemplate>
                                   <%--     <asp:dropdownlist ID="ddlplant" runat="server" DataTextField='<%# Bind("_plant") %>' DataValueField='<%# Bind("_plant") %>' ReadOnly="false" Width="150px" ForeColor="Black" MaxLength="6" EnableViewState="true" AppendDataBoundItems="true" ></asp:dropdownlist>                                      --%>
                                    <asp:DropDownList ID="ddlplant" width="135px" runat="server" AutoPostBack="false"></asp:DropDownList>                                        
                                     </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Event">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemTemplate>
                                       <%-- <asp:dropdownlist ID="ddlevent" runat="server" DataTextField='<%# Bind("_event") %>' DataValueField='<%# Bind("_plant") %>' ReadOnly="false" Width="150px" ForeColor="Black" MaxLength="6" EnableViewState="true" AppendDataBoundItems="true" ></asp:dropdownlist>                                      --%>
                                    <asp:DropDownList ID="ddlevent" width="135px" runat="server" AutoPostBack="false"></asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns> </asp:GridView>  

【问题讨论】:

  • 需要将选中的下拉值存入数据库中才能再次获取

标签: c# asp.net session gridview drop-down-menu


【解决方案1】:

希望您将 DataTable 绑定到 GridView。

保存您绑定到 GridView 的数据表并更新相同的数据表,如下所示。

gvwAssociation_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
 DataTable dt = (DataTable)Session["SavedDataTable"];
    foreach (GridViewRow gvRow in gvwAssociation.Rows)
    {
        DropDownList ddlpool = (DropDownList)gvRow.FindControl("ddlpool");
        DropDownList ddlyear = (DropDownList)gvRow.FindControl("ddlyear");
        if (dt.Select("ID=" + gvRow.Cells[1].Text).Length > 0)
        {
            dt.Select("ID=" + gvRow.Cells[1].Text)[0]["Pool"] = ddlpool.SelectedValue;
            dt.Select("ID=" + gvRow.Cells[1].Text)[0]["Year"] = ddlyear.SelectedValue; 
        }            
    }
    Session["SavedDataTable"] = dt;

    gvwAssociation.PageIndex = e.NewPageIndex;
    gvwAssociation.DataSource = dt;
    gvwAssociation.DataBind();
}

【讨论】:

  • 感谢您的回复,您的解决方案看起来不错,但是我在代码 dt.Select("ID=" + gvRow.Cells[1].Text 的 if 语句中遇到空引用错误).Length > 0) 感谢您的帮助。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多