此例子是基于c#的Asp.net
上网找了很多动态生成table例子,发现太简陋,笔者在此写一个完整版的...效果如下:
1.输入商品种类数,动态生成需要的行数,在每一个单元格可以填写相应的信息,
2.其中单价,和入库数量只能填写数字,金额是只读框(因为金额=商品数量*单价,用户没有必要填写)
3.当填写完整一行信息(商品名称,数量,金额),将会自动计算金额数..
文件结构如下图示:
MerchandiseInfo.cs是商品的结构类.
MerchandiseType.cs是商品类型的结果类.
TableCellModle.cs 是技术重点,此类中有生成各种单元格(TableCell)的方法,for example:
具有下拉列表的单元格方法(CreateCellddl),生成列标题的单元格方法(C
reateHeadCell)生成列标题的不同颜色单元格方法(CreateHeadCellColo
r)...
AotuCreatTable.aspx/cs是显示table的页面.
代码:MerchandiseInfo.cs
using System;
using System.Collections.Generic;
using System.Text;
public class MerchandiseInfo
{
/// 商品编号
private string m_ID;
public string ID
{
get { return m_ID; }
set { m_ID = value; }
}
/// 商品名称
private string m_Name;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
/// 商品数量
private int m_Number;
public int Number
{
get { return m_Number; }
set { m_Number = value; }
}
/// 商品分类
private string m_TID;
public string TID
{
get { return m_TID; }
set { m_TID = value; }
}
/// 金额
private decimal m_Amount;
/// 金额
public decimal Amount
{
get { return m_Amount; }
set { m_Amount = value; }
}
/// 单价
private decimal m_Price;
public decimal Price
{get { return m_Price; }
set { m_Price = value; }
}
/// 备注
private string m_Remark;
public string Remark
{
get { return m_Remark; }
set { m_Remark = value; }
}
/// 全赋值构造函数
public MerchandiseInfo(string pName, decimal pPrice,
int pNumber, string pTID, decimal pAmount, string pRemark)
{
m_Name = pName;
m_TID = pTID;
m_Amount = pAmount;
m_Price = pPrice;
m_Number = pNumber;
m_Remark = pRemark;
}
}
MerchandiseType.cs
public class MerchandiseType
{
/// 商品类型名称
private string m_Name;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
/// 商品类型ID
private string m_ID;
public string ID
{
get { return m_ID; }
set { m_ID = value; }
}
/// 全参函数
public MerchandiseType(string pID, string pName)
{ m_ID = pID;
m_Name = pName;
}
}
TableCellModle.cs
/// 列表的单元格类
public class TableCellModle
{
private TableCell tablecell = new TableCell();
private TextBox textbox = new TextBox();
private HiddenField hiField;
private DropDownList ddl;
public TableCellModle()
{}
/// 具有下拉列表的
public TableCell CreateCellddl(string tID, string tText, int tPoint, int twidth, int tHeight, int lwidth, int lHeight, ref IList<MerchandiseType> MTypeList)
{
ddl = new DropDownList();
ddl.DataSource = MTypeList;
ddl.DataTextField = "Name";
ddl.DataValueField = "ID";
ddl.DataBind();
ddl.AutoPostBack = false;
ddl.ID = tID ;
ddl.Font.Size = FontUnit.Point(tPoint);
ddl.Width = twidth;
ddl.Height = tHeight;
ddl.BorderWidth = 0;
tablecell.Controls.Add(ddl);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
return tablecell;
}
/// 创建题眉
public TableCell CreateHeadCell(string tID, string tText, int lwidth, int lHeight)
{
textbox.ID = tID;
textbox.Text = tText;
textbox.Font.Size = FontUnit.Point(13);
textbox.Width = 150;
textbox.Height = 20;
textbox.BorderWidth = 0;
textbox.BackColor = System.Drawing.Color.Yellow;
textbox.ReadOnly = true;
tablecell.Controls.Add(textbox);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
tablecell.VerticalAlign = VerticalAlign.Middle;
tablecell.Font.Size = FontUnit.Point(13);
tablecell.Font.Bold = true;
tablecell.HorizontalAlign = HorizontalAlign.Center;
tablecell.BorderColor = System.Drawing.Color.Black;
tablecell.BackColor = System.Drawing.Color.Yellow;
return tablecell;
}
/// 创建头Cell(可以改变颜色)
public TableCell CreateHeadCellColor(string tID, string tText, int lwidth, int lHeight, System.Drawing.Color pColor)
{
textbox.ID = tID;
textbox.Text = tText;
textbox.Font.Size = FontUnit.Point(13);
textbox.Width = 150;
textbox.Height = 20;
textbox.BorderWidth = 0;
textbox.BackColor = pColor;
textbox.ReadOnly = true;
tablecell.Controls.Add(textbox);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
tablecell.VerticalAlign = VerticalAlign.Middle;
tablecell.Font.Size = FontUnit.Point(13);
tablecell.Font.Bold = true;
tablecell.HorizontalAlign = HorizontalAlign.Center;
tablecell.BorderColor = System.Drawing.Color.Black;
tablecell.BackColor = pColor;
return tablecell;
}
/// 创建可读Cell
public TableCell CreateCellBeRead(string tID, string tText, int tPoint, int twidth, int tHeight, int lwidth, int lHeight, bool pReadOnly, bool pAutoPostBack)
{
textbox.ID = tID;
textbox.Text = tText;
textbox.Font.Size = FontUnit.Point(tPoint);
textbox.Width = twidth;
textbox.Height = tHeight;
textbox.BorderWidth = 0;
textbox.ReadOnly = pReadOnly;
textbox.AutoPostBack = pAutoPostBack;
tablecell.Controls.Add(textbox);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
return tablecell;
}
/// 创建隐藏文本
public TableCell CreateCellHiField(string tID, string tText, int lwidth, int lHeight)
{
hiField.ID = tID;
hiField.Value = tText;
tablecell.Controls.Add(hiField);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
return tablecell;
}
///同时 创建带隐藏域和文本的Cell
public TableCell CreateCell(string tID, string tText, int tPoint, int twidth, int tHeight, int lwidth, int lHeight, string pTaxRate, string pMID, string pPrice, string pPDID, string pID)
{
hiField = new HiddenField();
textbox.ID = tID;
textbox.Text = tText;
hiField.ID = tID + "TaxRate";
hiField.Value = pTaxRate;
textbox.Font.Size = FontUnit.Point(tPoint);
textbox.Width = twidth;
textbox.Height = tHeight;
textbox.BorderWidth = 0;
textbox.ReadOnly = true;
textbox.AutoPostBack = true;
tablecell.Controls.Add(hiField);
tablecell.Controls.Add(textbox);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.BorderWidth = 1;
return tablecell;
}
///创建可改变行数的隐藏域和文本的Cell
public TableCell CreateCellChangeColumn(string tID, string tText, int tPoint, int twidth, int tHeight, int lwidth, int lHeight, int ColumnSpan)
{
hiField = new HiddenField();
textbox.ID = tID;
textbox.Text = tText;
textbox.Font.Size = FontUnit.Point(tPoint);
textbox.Width = twidth;
textbox.Height = tHeight;
textbox.BorderWidth = 0;
textbox.ReadOnly = true;
textbox.AutoPostBack = true;
tablecell.Controls.Add(textbox);
tablecell.Width = lwidth;
tablecell.Height = lHeight;
tablecell.ColumnSpan = 4;
tablecell.BorderWidth = 1;
tablecell.HorizontalAlign = HorizontalAlign.Center;
tablecell.VerticalAlign = VerticalAlign.Middle;
tablecell.BorderColor = System.Drawing.Color.Black;
return tablecell;
}
}
TableList.cs
// 自定义表格
/// 商品明细列表
public class TableList
{
/// 生成商品表格
public void CreateTable(Table Table1, int rows, ref IList<MerchandiseType> MTypeList)
{//如果行数小于(0)则返回
if (rows<0)
return;
TableRow tr3 = new TableRow();
tr3.Cells.Add(new TableCellModle().CreateHeadCellColor("cells1" + Table1.Rows.Count, "商品名称", 150, 15, System.Drawing.Color.Azure));
tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells2" + Table1.Rows.Count, "单价", 150, 15));
tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells3" + Table1.Rows.Count, "入库数量", 75, 15));
tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells4" + Table1.Rows.Count, "商品类型", 150, 15));
tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells5" + Table1.Rows.Count, "金额", 150, 15));
tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells6" + Table1.Rows.Count, "备注", 150, 15));
// tr3.Cells.Add(new TableCellModle().CreateHeadCell("cells7" + Table1.Rows.Count, "隐藏域", 150, 15));
Table1.Rows.Add(tr3);
for (int i = 0; i < rows; i++)
{
TableRow tr = new TableRow();
tr.Cells.Add(new TableCellModle().CreateCellBeRead("cells1" + Table1.Rows.Count, "", 10, 75, 15, 75, 15,false,false ));
tr.Cells.Add(new TableCellModle().CreateCellBeRead("cells2" + Table1.Rows.Count, "", 10, 75, 15, 75, 15, false, false));
tr.Cells.Add(new TableCellModle().CreateCellBeRead("cells3" + Table1.Rows.Count, "", 10, 75, 15, 75, 15, false, true ));
//ref 作用是传地址,节省空间,
tr.Cells.Add(new TableCellModle().CreateCellddl("cells4" + Table1.Rows.Count, "", 10, 75, 15, 75, 15, ref MTypeList));
tr.Cells.Add(new TableCellModle().CreateCellBeRead("cells5" + Table1.Rows.Count, "", 10, 75, 15, 75, 15, true, false));
tr.Cells.Add(new TableCellModle().CreateCellBeRead("cells6" + Table1.Rows.Count, "", 10, 75, 15, 75, 15, false, false));
tr.Height = 15;
Table1.Rows.Add(tr);
tr.VerticalAlign = VerticalAlign.Middle;
tr.Font.Size = FontUnit.Point(13);
tr.Font.Bold = true;
tr.HorizontalAlign = HorizontalAlign.Center;
tr.BorderColor = System.Drawing.Color.Black;
}
TableRow tr1 = new TableRow();
tr1.Cells.Add(new TableCellModle().CreateCellChangeColumn("cellCount", "合计", 13, 150, 25, 150, 25, 4));
tr1.Cells.Add(new TableCellModle().CreateCellChangeColumn("cellSum","",13,150,25,150,25,0));
tr1.Height = 20;
Table1.Rows.Add(tr1);
tr1.VerticalAlign = VerticalAlign.Middle;
tr1.Font.Size = FontUnit.Point(13);
tr1.Font.Bold = true;
tr1.HorizontalAlign = HorizontalAlign.Center;
tr1.BorderColor = System.Drawing.Color.Black;
}
#region 订单求和计算
public string SumAmount(string oPrice, string oNumber)
{
decimal price = 0;
int number = 0;
string amonut = null;
if (string.IsNullOrEmpty(oPrice))
{price = 0;}
else
{ price = Convert.ToDecimal(oPrice); }
if (string.IsNullOrEmpty(oNumber))
{ number = 0; }
else
{number = Convert.ToInt32(oNumber); }
amonut = Convert.ToString(price * number );
return amonut;
}
#endregion
}
AotuCreatTable.aspx
<body> <form id="form1" runat="server"><div><fieldset style="width: 80%; text-align: center;"><legend><aspabel ID="lblMessage" runat="server" Text="填写入库信息及明细信息"></aspabel></legend> <br />
<table border="0" cellpadding="0" cellspacing="0" width="80%">
<tr><td align="right" nowrap="nowrap" style="height: 18px"> 输入商品种类数:</td>
<td align="left" nowrap="nowrap" style="height: 18px"> <asp:TextBox ID="txtNumber" runat="server" AutoPostBack="True"></asp:TextBox></td>
<td align="right" nowrap="nowrap" style="height: 18px"></td>
<td align="left" nowrap="nowrap" style="height: 18px">
</td>
<td align="right" nowrap="nowrap" style="height: 18px">
</td>
</tr>
<tr>
<td style="height: 6px">
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" style="height: 23px">
</td>
<td align="left" nowrap="nowrap" style="height: 23px">
</td>
<td align="right" nowrap="nowrap" style="height: 23px">
</td>
<td align="left" nowrap="nowrap" style="height: 23px">
</td>
<td align="right" nowrap="nowrap" style="height: 23px">
</td>
</tr>
</table>
<br />
在 <asp:Table ID="Table1" runat="server" BorderColor="black" BorderWidth="1" CellPadding="0"
CellSpacing="0" Width="80%" EnableViewState="False">
</asp:Table>
</fieldset>
<table border="0" cellpadding="0" cellspacing="0" width="80%">
<tr>
<td style="width: 10%">
</td>
<td align="left" style="text-align: center">
<asp:Button ID="btnRead" runat="server" Height="33px" Text="读取数据" OnClick="btnRead_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
AotuCreatTable.aspx.cs
public partial class opera_AddEnterWarehouse : System.Web.UI.Page
{
/// 入库单的详细列表table
private TableList tablelist = new TableList();
/// 入库的详细List
private IList<MerchandiseInfo> MerchandiseList = new List<MerchandiseInfo>();
protected void Page_Load(object sender, EventArgs e)
{
showEWETableList();
}
// 显示表格
private void showEWETableList()
{
Table1.Rows.Clear();
Table1.Controls.Clear();
IList<MerchandiseType> MTypeList = new List<MerchandiseType>();
MTypeList.Add(new MerchandiseType("1", "忌食品"));
MTypeList.Add(new MerchandiseType("2", "毒奶粉"));
MTypeList.Add(new MerchandiseType("3", "伪劣饮料"));
if (string.IsNullOrEmpty(txtNumber.Text) || !Regex.IsMatch(txtNumber.Text, @"^/d+$"))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>alert('错误,在行数输入非数字字符!')</script>");
return;
}
int rows = Convert.ToInt32(txtNumber.Text);
tablelist.CreateTable(Table1, rows, ref MTypeList);
for (int i = 1; i <= rows; i++)
{
//为每一个数量表加入事件
((TextBox)Table1.Rows.Cells[2].FindControl("cells3" + i)).TextChanged += new EventHandler(tablelist_TextChanged);
}
}
//输入数目改变事件
protected void tablelist_TextChanged(object sender, EventArgs e)
{//计算总和
Sumtablelist();
}
//计算总和
private void Sumtablelist()
{
decimal amount = 0; // 订单明细一条记录数据求和
decimal sum = 0; // 对订单明细所有记录金额求总和
int rows = Convert.ToInt32(txtNumber.Text);
for (int i = 1; i < rows + 1; i++)
{
string strNumber = ((TextBox)Table1.Rows.Cells[2].FindControl("cells3" + i)).Text;
string strPrice = ((TextBox)Table1.Rows.Cells[1].FindControl("cells2" + i)).Text;
#region
if (string.IsNullOrEmpty(strPrice) || string.IsNullOrEmpty(strNumber))
break ;
if (!Regex.IsMatch(strPrice, @"^/d+$"))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>alert('错误,在价格中输入非数字字符!')</script>");
((TextBox)Table1.Rows.Cells[1].FindControl("cells2" + i)).Text = "0";
strNumber = "0";
break;
}
if (!Regex.IsMatch(strNumber, @"^/d+$"))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>alert('错误,在数量中输入非数字字符!')</script>");
((TextBox)Table1.Rows.Cells[2].FindControl("cells3" + i)).Text = "0";
strNumber = "0";
break;
}
#endregion
string Name = ((TextBox)Table1.Rows.Cells[0].FindControl("cells1" + i)).Text;
if (!string.IsNullOrEmpty(Name))
{
string strSum = tablelist.SumAmount(strPrice, strNumber);
((TextBox)Table1.Rows.Cells[4].FindControl("cells5" + i)).Text = "¥" + strSum;
amount = Convert.ToDecimal(strSum);
sum += amount;
}
}
//最后一行是统计总价格.
((TextBox)Table1.Rows[rows+1].Cells[1].FindControl("cellSum")).Text = "¥" + Convert.ToString(sum);
this.btnRead.Enabled = true;
}
//读取数据
protected void btnRead_Click(object sender, EventArgs e)
{
//因为第0行是标题所以要从第1行开始
for (int i = 1; i < Convert.ToInt32(txtNumber.Text) + 1; i++)
{
string Name = ((TextBox)Table1.Rows.Cells[0].FindControl("cells1" + i )).Text;
string Price = ((TextBox)Table1.Rows.Cells[1].FindControl("cells2" + i )).Text;
string strNumber = ((TextBox)Table1.Rows.Cells[2].FindControl("cells3" + i)).Text;
string MType = ((DropDownList)Table1.Rows.Cells[3].FindControl("cells4" + i)).SelectedValue;
string Amount = ((TextBox)Table1.Rows.Cells[4].FindControl("cells5" + i )).Text.Replace("¥", string.Empty);//把¥替换
string Remark = ((TextBox)Table1.Rows.Cells[5].FindControl("cells6" + i)).Text;
if (!(strNumber.Equals("0") || string.IsNullOrEmpty(strNumber) || string.IsNullOrEmpty(Price) ||
string.IsNullOrEmpty(Amount) || string.IsNullOrEmpty(Name)||string.IsNullOrEmpty(MType)))
{
MerchandiseInfo MInfo = new MerchandiseInfo(Name, Convert.ToDecimal(Price), Convert.ToInt32(strNumber), MType, Convert.ToDecimal(Amount ),
Remark);
MerchandiseList.Add(MInfo);
}
}
if (MerchandiseList == null || MerchandiseList.Count < 1)
return ;
///输出所有的商品..
for (int i = 0; i < MerchandiseList.Count; i++)
{
Response.Write(" 商品名称:" + MerchandiseList.Name + " 单价:" + MerchandiseList.Price + " 入库数量: " + "商品类型ID:" + MerchandiseList.TID + " 金额: " +
MerchandiseList.Amount + " 备注:" + MerchandiseList.Remark + "<br>");
}
}
}