在转发器控件中查找列的小计
Finding subtotal for column in repeater control
我无法找到转发器控件中 "price" 列的小计。它不计算小计。提前致谢!
这是转发器的标记:
<asp:Repeater ID="rptItem" runat="server" OnItemCommand="rptItem_ItemCommand" OnItemDataBound="rptItem_ItemDataBound">
<HeaderTemplate>
<table id="carttable">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%# Eval("ProductId") %>' /></td>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' CssClass="imagecart" /></td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("ProductName") %>' NavigateUrl='<%# String.Concat("ProductDetails.aspx?ProductId=", Eval("ProductId")) %>'></asp:HyperLink></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:C}", Eval("Price")) %>'></asp:Label></td>
<td>
<asp:TextBox ID="txtQty" runat="server" Width="50"></asp:TextBox><asp:Button ID="btnUpdate" runat="server" Text="update" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
Subtotal:
<asp:Label ID="lblSubtotal" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:Repeater>
下面是代码:
protected void rptItem_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
List<Item> items = Session["Cart"] as List<Item>;
var productId = Convert.ToInt32(e.CommandArgument);
var itemToDelete = (from Item i in items
where i.ProductId == productId
select i).FirstOrDefault();
items.Remove(itemToDelete);
Session["Cart"] = items;
}
BindData();
}
protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
double subtotal = 0;
double price = 0;
if (e.Item.ItemType == ListItemType.Item)
{
price += Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
subtotal += price;
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblSubtotal = (Label)e.Item.FindControl("lblSubtotal");
lblSubtotal.Text = subtotal.ToString();
}
}
在 ItemDataBound
事件之外声明变量。您的代码的问题是 ItemDataBound 事件在每一行绑定到转发器控件后被调用,所以每次您的 subtotal
& price
都被初始化为 0
因此您没有得到预期的输出。
double subtotal = 0;
double price = 0;
protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
price += Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
subtotal += price;
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblSubtotal = (Label)e.Item.FindControl("lblSubtotal");
lblSubtotal.Text = subtotal.ToString();
}
}
我无法找到转发器控件中 "price" 列的小计。它不计算小计。提前致谢!
这是转发器的标记:
<asp:Repeater ID="rptItem" runat="server" OnItemCommand="rptItem_ItemCommand" OnItemDataBound="rptItem_ItemDataBound">
<HeaderTemplate>
<table id="carttable">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%# Eval("ProductId") %>' /></td>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' CssClass="imagecart" /></td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("ProductName") %>' NavigateUrl='<%# String.Concat("ProductDetails.aspx?ProductId=", Eval("ProductId")) %>'></asp:HyperLink></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:C}", Eval("Price")) %>'></asp:Label></td>
<td>
<asp:TextBox ID="txtQty" runat="server" Width="50"></asp:TextBox><asp:Button ID="btnUpdate" runat="server" Text="update" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
Subtotal:
<asp:Label ID="lblSubtotal" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:Repeater>
下面是代码:
protected void rptItem_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
List<Item> items = Session["Cart"] as List<Item>;
var productId = Convert.ToInt32(e.CommandArgument);
var itemToDelete = (from Item i in items
where i.ProductId == productId
select i).FirstOrDefault();
items.Remove(itemToDelete);
Session["Cart"] = items;
}
BindData();
}
protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
double subtotal = 0;
double price = 0;
if (e.Item.ItemType == ListItemType.Item)
{
price += Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
subtotal += price;
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblSubtotal = (Label)e.Item.FindControl("lblSubtotal");
lblSubtotal.Text = subtotal.ToString();
}
}
在 ItemDataBound
事件之外声明变量。您的代码的问题是 ItemDataBound 事件在每一行绑定到转发器控件后被调用,所以每次您的 subtotal
& price
都被初始化为 0
因此您没有得到预期的输出。
double subtotal = 0;
double price = 0;
protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
price += Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
subtotal += price;
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblSubtotal = (Label)e.Item.FindControl("lblSubtotal");
lblSubtotal.Text = subtotal.ToString();
}
}