简体   繁体   English

即使页面索引正在更改,如何保留多个下拉列表的选定值?

[英]How to retain multiple dropdown list's selected value even when page index is changing?

I'm a newbie in asp.net development. 我是asp.net开发的新手。 Any help would be much appreciated. 任何帮助将非常感激。 :) What I need to accomplish is to retain the selected value of my drop down lists in my gridview even when the user navigate to the next page index. :)我需要完成的是即使用户导航到下一页索引,也要在网格视图中保留下拉列表的选定值。 I am thinking to put the values into session while page are changing and put it back when the page shows again. 我正在考虑在页面更改时将值放到会话中,并在页面再次显示时放回去。 I have tried to do this in check box only and I don't know how to implement using drop down lists. 我只尝试在复选框中执行此操作,但我不知道如何使用下拉列表来实现。 I have 4 drop down lists by the way. 顺便说一下,我有4个下拉列表。 Please help. 请帮忙。 Thanks a lot. 非常感谢。 Below is my code 下面是我的代码

 <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>  

Hope you binding a DataTable to GridView. 希望您将DataTable绑定到GridView。

Save the Data Table which you are binding to the GridView and Update the Same DataTable something like beow. 保存要绑定到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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改下拉列表中的选定值时如何显示弹出窗口? - How to diplay a popup window when changing the selected value in a dropdown list? 使用MVC,C#和AngularJS时如何在页面加载之间保留页面的下拉值? - How to retain a page's dropdown value across page loads when using MVC, C# and AngularJS? 由于下拉列表的选定索引更改,标签在发回邮件时不会保留其文本 - Labels don't retain their text when Post back happenes because of a selected Index Change of a dropdown list 如何在选定索引更改的grideview的下拉列表中显示值 - How to display in value in dropdown list in grideview on selected index change 模板字段中下拉列表的选定值未更改 - selected value of dropdown list not changing in template field 如何禁用上一页中选择的当前表单中的下拉列表值 - How to disable dropdown list value in the current form that was selected in the previous page 如何在页面更改时保留下拉列表中的选定值? - How to keep the selected value from the dropdown when the page is changed? 选定索引更改后下拉列表返回第一个值 - dropdown list goes back to first value after selected index changes 选择下拉值时如何取消选择单选按钮 - How to deselect radiobutton when dropdown value is selected 如何从MVC视图将下拉列表选择的值发送到控制器的变量ViewData? - How to send a dropdown list selected value from MVC view to controller's variable ViewData?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM