我试图从可折叠面板扩展器那样做,我通过选中复选框选择几行。在我标记它们并单击添加按钮后,我将它们存储到列表中并在gridview中显示添加的项目。之后,我进入了另一个使用转发器设置的gridview,我选择了另外几个项目,然后它将被添加到列表中,之前添加的项目仍然存在。以下是我使用转发器设置gridview的方法:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<!-- COLLAPSIBLE PANEL EXTENDER -->
<asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
<!-- Collapsible panel extender header -->
<div class="form-group" style="background-color: #ffb848; height: 30px; vertical-align: middle">
<div class="col-md-6">
<div style="float: left; color: White; padding: 5px 5px 0 0">
<asp:Label ID="lblProduct" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' runat="server" />
</div>
</div>
<div class="col-md-6">
<div style="float: right; color: White; padding: 5px 5px 0 0">
<asp:Label ID="lblHeaderText1" runat="server" />
</div>
</div>
<div style="clear: both"></div>
</div>
</asp:Panel>
<!-- Collapsible panel extender body -->
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
<asp:Label ID="lblBodyText1" runat="server" />
<!-- Grid view to show products based on each category -->
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="740px" CellPadding="2" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbCheckRow" runat="server" ItemStyle-Width="50px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="Name" ItemStyle-Width="50px" />
<asp:BoundField DataField="description" HeaderText="Name" ItemStyle-Width="550px" />
<asp:BoundField DataField="halal" HeaderText="Halal" ItemStyle-Width="50px" />
<asp:BoundField DataField="unitQuantity" HeaderText="Unit Quantity" />
</Columns>
</asp:GridView>
<br />
</asp:Panel>
<asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
ExpandControlID="pHeader1" Collapsed="true" TextLabelID="lblHeaderText1" CollapsedText="Show"
ExpandedText="Hide" CollapsedSize="0"
ScrollContents="false">
</asp:CollapsiblePanelExtender>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Panel ID="pBody2" runat="server" CssClass="cpBody">
<asp:GridView ID="gvFinalised" runat="server" AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" Width="740px" DataKeyNames="id">
<Columns>
<asp:BoundField DataField="categoryName" HeaderText="Category" />
<asp:BoundField DataField="name" HeaderText="Product" />
<asp:BoundField DataField="inventoryQuantity" HeaderText="Storage Level" />
<asp:BoundField DataField="unitQuantity" HeaderText="Unit Quantity" />
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200px">
<ItemTemplate>
<asp:TextBox ID="tbQuantity" runat="server" Width="40" Text="0" OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true" />
<asp:Label ID="lblCheckAmount" runat="server" ForeColor="#a94442"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
有两个网格视图。一个转发器内部显示产品和产品变体。另一个在转发器之外,显示添加到其中的项目。而背后的代码:
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<ProductPacking> prodVariantDetail = new List<ProductPacking>();
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
if (cb.Checked)
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
Label1.Text += prodVariantIDList[i];
prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
}
我的问题是让我说我选择的产品变体1,2,3属于罐头食品类别。然后我去了另一个类别并选择了产品变体4,5,6。最新动作4,5,6将显示在gridview中,但不会显示在1,2,3中。我想知道如何解决这个问题?我是否需要临时列表来存储它?
提前致谢。
编辑部分
private List<ProductPacking> prodVariantDetail
{
get
{
if (ViewState["prodVariantDetail"] == null)
{
return new List<ProductPacking>();
}
else
{
return (List<ProductPacking>)ViewState["prodVariantDetail"];
}
}
set
{
ViewState["prodVariantDetail"] = value;
}
}
答案 0 :(得分:0)
执行prodVariantIDList
时,问题lbnAdd_Click
将始终为空,因此您需要在回发之间保留以前选择的产品变体ID。我建议使用ViewState存储prodVariantIDList
。
修改强>
查看已编辑的部分后,如果List<ProductPacking>
类不可序列化,则无法在ViewState中存储ProductPacking
。我建议一个更简单的解决方案。首先更改属性:
private List<string> SelectedVariantDetailIDs
{
get
{
if (ViewState["SelectedVariantDetailIDs"] == null)
{
return new List<string>();
}
else
{
return (List<string>)ViewState["SelectedVariantDetailIDs"];
}
}
set
{
ViewState["SelectedVariantDetailIDs"] = value;
}
}
然后在lbnAdd_Click
:
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<ProductPacking> prodVariantDetail = new List<ProductPacking>();
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
if (cb.Checked)
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
Label1.Text += prodVariantIDList[i];
prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}