我是asp.net的新手,我正在尝试制作购物车。我有一个商店设置,有一个学校用品清单和一个复选框旁边的一个"添加到购物车"底部的按钮。我想只将已检查的项目存储到会话中,并将其调用到我的购物车页面上的另一个gridview。出于某种原因,我的购物车页面上的gridview没有出现。
Shop.aspx
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Price" DataFormatString="{0:c2}" HeaderText="Price" SortExpression="Price" />
<asp:TemplateField HeaderText="Add To Cart">
<ItemTemplate>
<asp:CheckBox ID="cbAdd" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table] ORDER BY [Id], [Name], [Price]"></asp:SqlDataSource>
</div>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add To Cart" OnClick="btnAdd_Click" />
Shop.aspx.cs
protected void Page_Load(object sender, EventArgs e)//bind list to gridview
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();//create new datatable
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Country") });
foreach (GridViewRow row in gvProducts.Rows)//for each row in the gridview
{
CheckBox ckRow = (CheckBox)row.FindControl("ckAdd");
if (ckRow !=null && ckRow.Checked)
{
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
Session["ShoppingCart"] = dt;//store datatable as session called ShoppingCart
}
Response.Redirect("~/ShoppingCart.aspx");
}
ShoppingCart.aspx
<h1>Shopping Cart</h1>
<asp:GridView ID="gvProductsList" runat="server" AutoGenerateColumns="true" ViewStateMode="Enabled">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Textbox ID="txtQuantity" runat="server" Text='<%# Bind("Count")%>'></asp:Textbox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Total">
<ItemTemplate>
<asp:Label ID="lblProductTotal" runat="server" Text='<%# Convert.ToInt32(Eval("Quantity"))*Convert.ToInt32(Eval("Price"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Order Total: "></asp:Label>
</FooterTemplate>
</asp:TemplateField>
ShoppingCart.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = (DataTable)Session["ShoppingCart"];
gvProductsList.DataSource = dt;
gvProductsList.DataBind();
}
}
答案 0 :(得分:0)
试试这个: -
List<GridViewRow> gridcheckedrows = gvProducts.Rows.Cast<GridViewRow>().
Where(x => (x.FindControl("cbAdd") as CheckBox)
.Checked == true).ToList();
我使用下面的自定义数据来绑定网格gvProducts
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "Sharique";
dr[2] = "120";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "2";
dr[1] = "Ansari";
dr[2] = "100";
dt.Rows.Add(dr);
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
}
使用它来绑定网格gvProductsList
List<GridViewRow> gridcheckedrows = gvProducts.Rows.Cast<GridViewRow>().Where(x => (x.FindControl("cbAdd") as CheckBox).Checked == true).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
dt.Columns.Add("Count");
dt.Columns.Add("Quantity");
foreach (GridViewRow gvr in gridcheckedrows)
{
DataRow dr = dt.NewRow();
dr["Id"] = gvr.Cells[0].Text;
dr["Name"] = gvr.Cells[1].Text;
dr["Price"] = gvr.Cells[2].Text;
dr["Count"] = "1";
dr["Quantity"] = "2";
dt.Rows.Add(dr);
}
gvProductsList.DataSource = dt;
gvProductsList.DataBind();
和网格gvProductsList
使用html
<asp:GridView ID="gvProductsList" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Count")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Total">
<ItemTemplate>
<asp:Label ID="lblProductTotal" runat="server" Text='<%# ((Convert.ToInt32(Eval("Quantity")))*(Convert.ToInt32(Eval("Price"))))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Order Total: "></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
希望这会对你有所帮助。