Aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="electric.aspx.cs" Inherits="_1.electric" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
家电类<br />
<br />
<asp:Label ID="Label1" runat="server" Text="电视"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="40px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="冰箱"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="40px"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="洗衣机"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Width="40px"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="提交" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查看购物车" />
<br />
<br />
<asp:Table ID="Table1" runat="server"><!--table 在这儿-->
</asp:Table>
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="修改" />
</div>
</form>
</body>
</html>
后台处理:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;namespace _1
{
public partial class electric : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"] == null)
{
DataSet ds = new DataSet();
ds.Tables.Add("goods");
ds.Tables["goods"].Columns.Add("name");
ds.Tables["goods"].Columns.Add("count");
Session["goods"] = ds;
}
} protected void Button1_Click(object sender, EventArgs e)//显示购物车内容
{
DataSet ds = new DataSet();
ds = (DataSet)Session["goods"];
for (int i = 0; i < ds.Tables["goods"].Rows.Count; i++)
{
TableRow tr=new TableRow();
TableCell tc1=new TableCell();
TableCell tc2 = new TableCell();
TextBox tx1 = new TextBox();
TextBox tx2 = new TextBox();
tx1.Text = ds.Tables["goods"].Rows[i]["name"].ToString();
tx2.Text = ds.Tables["goods"].Rows[i]["count"].ToString();
tc1.Controls.Add(tx1);
tc2.Controls.Add(tx2);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
Table1.Rows.Add(tr);
}
} protected void Button2_Click(object sender, EventArgs e)//添加商品
{
DataSet ds = new DataSet();
ds.Tables.Add("goods");
ds.Tables["goods"].Columns.Add("name");
ds.Tables["goods"].Columns.Add("count");
ds.Tables["goods"].Rows.Add(new object[] { Label1.Text, TextBox1.Text });
ds.Tables["goods"].Rows.Add(new object[] { Label2.Text, TextBox2.Text });
ds.Tables["goods"].Rows.Add(new object[] { Label3.Text, TextBox3.Text });
Session["goods"] = ds;
} protected void Button3_Click(object sender, EventArgs e)//修改
{
DataSet ds = new DataSet();
ds.Tables.Add("goods");
ds.Tables["goods"].Columns.Add("name");
ds.Tables["goods"].Columns.Add("count");
for (int i = 0; i <this.Table1.Rows.Count; i++)
{
Response.Write(Table1.Rows[i].Cells.Count+"------------");//为0啊啊啊啊 啊啊 啊啊 !!!
TextBox tb1=(TextBox)this.Table1.Rows[i].Cells[0].Controls[0];//??????????????????????????
TextBox tb2=(TextBox)Table1.Rows[i].Cells[0].Controls[1];
ds.Tables["goods"].Rows.Add(new object[] {tb1.Text , tb2.Text });
}
Session["goods"] = ds;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货