C# 如何从<;获取文本框;项目模板>;中继器ASP网
我有一个产品目录页。当您单击“添加产品”按钮时,将显示带有购物车的页面。此页面有一个表,其中包含ItemTemplate。是否有任何方法可以从位于ItemTemplate内的文本框中获取值,并通过单击按钮更改表中Total cost列中的值?主要问题是,我无法访问文本框,因为它位于。多谢各位 目录页: 购物车页面: CartView.aspxC# 如何从<;获取文本框;项目模板>;中继器ASP网,c#,asp.net,C#,Asp.net,我有一个产品目录页。当您单击“添加产品”按钮时,将显示带有购物车的页面。此页面有一个表,其中包含ItemTemplate。是否有任何方法可以从位于ItemTemplate内的文本框中获取值,并通过单击按钮更改表中Total cost列中的值?主要问题是,我无法访问文本框,因为它位于。多谢各位 目录页: 购物车页面: CartView.aspx MasterPageFile="~/Page/Store.Master" %> <asp:Content ID=&q
MasterPageFile="~/Page/Store.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodyContent" runat="server">
<div id="content" style="margin-left: 7%;">
<style>
@import url("/css/tableCart.css");
@import url("/css/ButtonsCart.css");
</style>
<h2 style="padding: 14px; color:Highlight;">Ваша корзина</h2>
<h3 style="padding: 14px; color:Highlight;">Товары, которые вы добавили в корзину, представлены здесь</h3>
<table id="Table1" V class ="simple-little-table">
<thead>
<tr>
<th></th>
<th>Название</th>
<th>Цвет</th>
<th>Глубина</th>
<th>Ширина</th>
<th>Цена</th>
<th>Количество</th>
<th>Итого</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" ItemType="Line.Models.CartLine"
SelectMethod="GetCartLines" runat="server" EnableViewState="false">
<ItemTemplate>
<tr>
<td><asp:Image ID="Image1" runat="server" style="height:45px; " ImageUrl=<%# Item.Product.Img %> /></td>
<td> <%# Item.Product.NameProduct %> <%# Item.Product.TypeProducts %></td>
<td><%# Item.Product.Colors %></td>
<td>
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<asp:Button ID="cmdUpdate" OnClick="cmdUpdate_Click1" runat="server" Text="Update" CommandName="MyUpdate" CommandArgument = '<%# Container.ItemIndex %>'/>
</td>
<td><%# Item.Size.Depth%></td>
<td><%# Item.Product.Price%></td>
</td>
<td>
<td>
<asp:Label ID="Label2" runat="server" Text="<%# ((Item.Quantity *
Item.Product.Price))%>"></asp:Label>
</td>
<td>
<asp:Label ID="txtAmount" runat="server" Text=""></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
<tr>
<td colspan="3">Итого:</td>
<td colspan="2" ><%= CartTotal.ToString("c") %></td>
</tr>
</tfoot>
</table>
</div>
</asp:Content>
MasterPageFile=“~/Page/Store.Master”%>
@导入url(“/css/tableCart.css”);
@导入url(“/css/ButtonsCart.css”);
Ваша корзина
Товары, которые вы добавили в корзину, представлены здесь
Название
Цвет
Глубина
Ширина
Цена
Количество
Итого
数量:
Итого:
CartView.aspx.cs
using Line.Helpers;
using Line.Models;
using Line.Models.Repository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Line.Page
{
public partial class CartView : System.Web.UI.Page
{
protected void Page_Load(object sender, RepeaterCommandEventArgs e)
{
}
public IEnumerable<CartLine> GetCartLines()
{
return SessionHelper.GetCart(Session).Lines;
}
public decimal CartTotal
{
get
{
return SessionHelper.GetCart(Session).ComputeTotalValue();
}
}
public string CheckoutUrl
{
get
{
return RouteTable.Routes.GetVirtualPath(null, "checkout",
null).VirtualPath;
}
}
public string ReturnUrl
{
get
{
return SessionHelper.Get<string>(Session, SessionKey.RETURN_URL);
}
}
protected void cmdUpdate_Click1(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items[Convert.ToInt32(e.CommandArgument)];
TextBox tQty = (TextBox)rRow.FindControl("txtQty");
Label tAmount = (Label)rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text;
}
}
}
}
使用Line.Helpers;
使用直线模型;
使用Line.Models.Repository;
使用制度;
使用System.Collections.Generic;
使用系统配置;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
使用System.Web.Routing;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间行.Page
{
公共部分类CartView:System.Web.UI.Page
{
受保护的无效页面加载(对象发送器、RepeaterCommandEventArgs e)
{
}
公共IEnumerable GetCartLines()
{
返回SessionHelper.GetCart(Session).line;
}
公共十进位总计
{
得到
{
返回SessionHelper.GetCart(Session.ComputeTotalValue();
}
}
公共字符串签出URL
{
得到
{
返回RouteTable.Routes.GetVirtualPath(null,“签出”,
null)。虚拟路径;
}
}
公共字符串返回URL
{
得到
{
return SessionHelper.Get(Session,SessionKey.return\u URL);
}
}
受保护的void cmdUpdate\u Click1(对象发送方、RepeaterCommandEventArgs e)
{
如果(e.CommandName==“MyUpdate”)
{
RepeaterItem rRow=Repeater1.Items[Convert.ToInt32(e.CommandArgument)];
TextBox tQty=(TextBox)错误查找控件(“txtQty”);
Label tAmount=(Label)rRow.FindControl(“txtAmount”);
tAmount.Text=tQty.Text;
}
}
}
}
好的,您可以设置按钮的索引,并在中继器的“项目命令”中选择该索引
因此,对于您的按钮,您可以/想要说:
我在中继器中有数量、价格和金额。还有按钮
因此,标记可以如下所示:
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// update button in repeater clicked
// update amount based on qty, and price
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items(e.CommandArgument);
TextBox tQty = rRow.FindControl("txtQty");
TextBox tPrice = rRow.FindControl("txtPrice");
TextBox tAmount = rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text * tPrice.Text;
}
}
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
<td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Eval("Active") %>' Enabled="false" /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">ID</th>
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Active</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
因此,只需按照上述步骤传递中继器行的“索引”。我们将此表达式用作命令参数:
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
所以上面的内容填充了repeater——正如前面提到的,对于类似网格的布局,我将使用listview。将列表视图拖动到网页中
使用向导连接到数据库。现在吹掉所有模板,只留下itemtemplate
加载网格的代码-同上
现在,根据前面的屏幕截图,取代中继器
你得到这个:
那么你的多条线就是你的建筑?它们应该基于listview并围绕listview。因此,每一行都可以被抓取,并像我在上面所做的那样处理
做一个谷歌的listview的例子-有一个像十亿个例子
一旦你有了这个设置,那么每一行都是一个“东西”,根据表中的数据自动重复
列表视图的标记——它同样遵循与repeator相同的思想和概念。上面的列表视图如下所示:
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// update button in repeater clicked
// update amount based on qty, and price
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items(e.CommandArgument);
TextBox tQty = rRow.FindControl("txtQty");
TextBox tPrice = rRow.FindControl("txtPrice");
TextBox tAmount = rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text * tPrice.Text;
}
}
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
<td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Eval("Active") %>' Enabled="false" /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">ID</th>
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Active</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
身份证件
名字
姓氏
酒店名称
城市
活跃的
非常感谢您的回复。但是您能告诉我,如何使用Repeater1.Items()作为方法吗?VS告诉我它只能用作属性。items是一个属性-它是行的集合-而且,is不是