[英]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.