62,046
社区成员
发帖
与我相关
我的任务
分享
<!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">
<center>
<div id = "center" >
<div id = "left" style=" width: 400px ">
<asp:Table ID="List_Table" runat="server">
</asp:Table>
</div>
<div id = "right" style =" width :400px; text-align:center ">
<asp:Button ID="btnAddList" runat="server" Text="添加" onclick="btnAddList_Click"
style="height: 26px" />
</div>
</div>
</center>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace LC.ApplyUser
{
public partial class ChooseGoods : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this.ViewState["table"] = List_Table;
this.ViewState["num"] = 1;
}
this.MyInit();
}
/// <summary>
/// 初始化 表格
/// </summary>
protected void MyInit()
{
TableRow tr = new TableRow();
TableHeaderCell thc;
thc = new TableHeaderCell();
thc.Text = "物品编号";
tr.Cells.Add(thc);
thc = new TableHeaderCell();
thc.Text = "物品名称";
tr.Cells.Add(thc);
thc = new TableHeaderCell();
thc.Text = "单价";
tr.Cells.Add(thc);
thc = new TableHeaderCell();
thc.Text = "数量";
tr.Cells.Add(thc);
thc = new TableHeaderCell();
thc.Text = "删除";
tr.Cells.Add(thc);
this.List_Table.Rows.Add(tr);
int temp = this.Num;
for (int i = 0; i < temp; ++i)
{
this.NewTableRow(i);
}
}
private int Num
{
get
{
return Convert.ToInt32(this.ViewState["num"]);
}
set
{
this.ViewState["num"] = value;
}
}
protected void NewTableRow(int i )
{
TableRow tr = new TableRow();
TableCell tc;
TextBox tb;
ListBox lb;
Button bt;
//物品编号
tc = new TableCell();
tb = new TextBox();
tb.CssClass = "apply";
tb.ReadOnly = true;
tb.ID = "txtBh" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);
//物品名称
tc = new TableCell();
tb = new TextBox();
tb.CssClass = "apply";
tb.ReadOnly = true;
tb.ID = "txtMc" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);
//单价
tc = new TableCell();
tb = new TextBox();
tb.CssClass = "apply";
tb.ReadOnly = true;
tb.ID = "txtDj" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);
//数量
tc = new TableCell();
lb = new ListBox();
tb.CssClass = "apply";
for (int k = 1; k <= 10; k++)
{
lb.Items.Add( Convert.ToString(k));
}
lb.Items.Add("15");
lb.Items.Add("20");
lb.Rows = 1;
lb.ID = "txtSl" + i;
tc.Controls.Add(lb);
tr.Cells.Add(tc);
//删除按钮
tc = new TableCell();
bt = new Button();
bt.CssClass = "apply";
bt.ID = "txtCc"+ i;
bt.Text = "删除";
bt.Click += new EventHandler(txtCc_Click);
tc.Controls.Add(bt);
tr.Cells.Add(tc);
this.List_Table.Rows.Add(tr);
}
//点击删除按钮 所调用的函数
protected void txtCc_Click(object sender, EventArgs e)
{
Response.Write("123"+"<br />");
Response.Write(e + "<br />");
Response.Write(sender);
}
//点击添加 按钮所调用的函数
protected void btnAddList_Click(object sender, EventArgs e)
{
this.NewTableRow(this.Num++);
}
}
}
protected void txtCc_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
TableRow tr = (TableRow) bt.Parent.Parent;
this.List_Table.Rows.Remove(tr);
}
//点击删除按钮 所调用的函数
protected void txtCc_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(ViewState["num"]) > 0)
{
Table1.Rows.RemoveAt(Table1.Rows.Count - 1);
ViewState["num"] = Convert.ToInt32(ViewState["num"]) - 1;
}
}
//点击添加 按钮所调用的函数
protected void btnAddList_Click(object sender, EventArgs e)
{
this.NewTableRow(this.Num++);
ViewState["num"] = Convert.ToInt32(ViewState["num"]) + 1;
}