如何在页面索引发生变化时保留多个下拉列表的选定值
本文关键字:保留 下拉列表 变化 索引 | 更新日期: 2023-09-27 17:50:52
我是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的数据表,并更新相同的数据表,如下所示。
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();
}