Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保留多个下拉列表';是否在页面索引更改时仍保留选定值?_C#_Asp.net_Session_Gridview_Drop Down Menu - Fatal编程技术网

C# 如何保留多个下拉列表';是否在页面索引更改时仍保留选定值?

C# 如何保留多个下拉列表';是否在页面索引更改时仍保留选定值?,c#,asp.net,session,gridview,drop-down-menu,C#,Asp.net,Session,Gridview,Drop Down Menu,我是asp.net开发的新手。任何帮助都将不胜感激。:) 我需要完成的是在gridview中保留下拉列表的选定值,即使用户导航到下一页索引。我正在考虑在页面更改时将值放入会话,在页面再次显示时将其放回会话。我只在复选框中尝试过这样做,我不知道如何使用下拉列表实现。顺便说一下,我有4个下拉列表。请帮忙。谢谢。下面是我的代码 <asp:GridView ID="gvwAssociation" runat="server" AutoGenerateColumns="False"

我是asp.net开发的新手。任何帮助都将不胜感激。:) 我需要完成的是在gridview中保留下拉列表的选定值,即使用户导航到下一页索引。我正在考虑在页面更改时将值放入会话,在页面再次显示时将其放回会话。我只在复选框中尝试过这样做,我不知道如何使用下拉列表实现。顺便说一下,我有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>  

--%>
--%>

希望您将DataTable绑定到GridView

保存要绑定到GridView的数据表,并更新相同的数据表,如beow

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的if语句中遇到了空引用错误。Select(“ID=“+gvRow.Cells[1].Text)。长度>0)感谢您的帮助。非常感谢