幾處瑕疵
1.正規表達式不适合,單價必須為整數
2.合并列處預設為4,無法配置。
基礎類定義:
/// <summary>
/// 商品資訊類
/// </summary>
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;
}
}
/// <summary>
/// 商品類型類
/// </summary>
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;
}
}
/// <summary>
/// 表格Cell模式建立類
/// </summary>
public class TableCellModle
{
private TableCell tablecell = new TableCell();
private TextBox textbox = new TextBox();
private HiddenField hiField = new HiddenField();
private DropDownList ddl;
public TableCellModle()
{ }
/// <summary>
/// 具有下拉清單的Cell
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tPoint">字型大小</param>
/// <param name="twidth">控件寬度</param>
/// <param name="tHeight">控件高度</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <param name="MTypeList">下拉框資料源</param>
/// <returns></returns>
public TableCell CreateCellddl(string tID, 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;
}
/// <summary>
/// 建立表頭
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <returns></returns>
public TableCell CreateHeadCell(string tID, string tText, int lwidth, int lHeight)
{
textbox.ID = tID;
textbox.Text = tText;
textbox.Font.Size = FontUnit.Point(13);
textbox.Height = 20;
textbox.BorderWidth = 0;
textbox.Style.Add("text-align", "center");
textbox.BackColor = System.Drawing.Color.Transparent;
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.Transparent;
return tablecell;
}
/// <summary>
/// 建立頭Cell(可以配置顔色)
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <param name="pColor">顔色</param>
/// <returns></returns>
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;
}
/// <summary>
/// 建立可讀寫Cell
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="tPoint">字型大小</param>
/// <param name="twidth">控件寬度</param>
/// <param name="tHeight">控件高度</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <param name="pReadOnly">是否隻讀</param>
/// <param name="pAutoPostBack">是否自動回調</param>
/// <returns></returns>
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;
}
/// <summary>
/// 建立隐藏文本
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <returns></returns>
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;
}
/// <summary>
/// 同時 建立帶隐藏域和文本的Cell
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="tPoint">字型大小</param>
/// <param name="twidth">控件寬度</param>
/// <param name="tHeight">控件寬度</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <param name="pTaxRate">pTaxRate</param>
/// <param name="pMID"></param>
/// <param name="pPrice"></param>
/// <param name="pPDID"></param>
/// <param name="pID"></param>
/// <returns></returns>
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)
{
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;
}
/// <summary>
/// ///建立可改變行數的隐藏域和文本的Cell
/// </summary>
/// <param name="tID">控件ID</param>
/// <param name="tText">控件文本</param>
/// <param name="tPoint">字型大小</param>
/// <param name="twidth">控件寬度</param>
/// <param name="tHeight">控件高度</param>
/// <param name="lwidth">Cell寬度</param>
/// <param name="lHeight">Cell高度</param>
/// <param name="ColumnSpan">合并列數</param>
/// <returns></returns>
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 = ColumnSpan;
tablecell.BorderWidth = 1;
tablecell.HorizontalAlign = HorizontalAlign.Center;
tablecell.VerticalAlign = VerticalAlign.Middle;
tablecell.BorderColor = System.Drawing.Color.Black;
return tablecell;
}
}
/// <summary>
/// 自定義表格類
/// </summary>
public class TableList
{
/// <summary>
/// 生成商品表格
/// </summary>
/// <param name="Table1">表格</param>
/// <param name="rows">表格行數</param>
/// <param name="MTypeList">表格中下拉框資料源</param>
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, 2));
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;
}
/// <summary>
/// 計算總價
/// </summary>
/// <param name="oPrice">單價</param>
/// <param name="oNumber">數量</param>
/// <returns></returns>
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;
}
/// <summary>
/// 重寫表格寬度
/// </summary>
/// <param name="Table1">表格</param>
public void ReWrite(Table Table1)
{
for (int r = 1; r < Table1.Rows.Count-1; r++)
{
for (int c = 0; c < Table1.Rows[r].Cells.Count; c++)
{
if ((Table1.Rows[r].Cells[c].FindControl("cells" + (c + 1) + r)).GetType() != typeof(TextBox)) { continue; }
TextBox tb = ((TextBox)Table1.Rows[r].Cells[c].FindControl("cells" + (c + 1) + r));
TableCell tc = ((TableCell)Table1.Rows[r].Cells[c]);
string text = tb.Text;
int coun1 = 0, textLength;
ArrayList itemList = new ArrayList();
CharEnumerator CEnumerator = text.GetEnumerator();
Regex regex = new Regex("^[\u4E00-\u9FA5]{0,}$");
while (CEnumerator.MoveNext())
{
if (regex.IsMatch(CEnumerator.Current.ToString(), 0))
itemList.Add(CEnumerator.Current.ToString());
coun1 = itemList.Count;
}
textLength = coun1 * 16 + (text.Length - coun1) * 8;
if (textLength > 150)
{
tb.Width = 250;
}
if (textLength > 250)
{
tb.Height = 30;
tb.TextMode = TextBoxMode.MultiLine;
}
}
}
}
}
表格實作:
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
showEWETableList();
}
//入庫單的詳細清單table
private TableList tablelist = new TableList();
//入庫的詳細List
private IList<MerchandiseInfo> MerchandiseList = new List<MerchandiseInfo>();
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[i].Cells[2].FindControl("cells3" + i)).TextChanged += new EventHandler(tablelist_TextChanged);
}
}
//輸入數目改變事件
protected void tablelist_TextChanged(object sender, EventArgs e)
{//計算總價
tablelist.ReWrite(Table1);
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[i].Cells[2].FindControl("cells3" + i)).Text;
string strPrice = ((TextBox)Table1.Rows[i].Cells[1].FindControl("cells2" + i)).Text;
#region
if (string.IsNullOrEmpty(strPrice) || string.IsNullOrEmpty(strNumber))
if (string.IsNullOrEmpty(strNumber))
break;
if (!Regex.IsMatch(strPrice, @"^\d+$"))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>alert('錯誤,在價格中輸入非數字字元!')</script>");
((TextBox)Table1.Rows[i].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[i].Cells[2].FindControl("cells3" + i)).Text = "0";
strNumber = "0";
break;
}
#endregion
string Name = ((TextBox)Table1.Rows[i].Cells[0].FindControl("cells1" + i)).Text;
if (!string.IsNullOrEmpty(Name))
{
string strSum = tablelist.SumAmount(strPrice, strNumber);
((TextBox)Table1.Rows[i].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[i].Cells[0].FindControl("cells1" + i)).Text;
string Price = ((TextBox)Table1.Rows[i].Cells[1].FindControl("cells2" + i)).Text;
string strNumber = ((TextBox)Table1.Rows[i].Cells[2].FindControl("cells3" + i)).Text;
string MType = ((DropDownList)Table1.Rows[i].Cells[3].FindControl("cells4" + i)).SelectedValue;
string Amount = ((TextBox)Table1.Rows[i].Cells[4].FindControl("cells5" + i)).Text.Replace("¥", string.Empty);//把¥替換
string Remark = ((TextBox)Table1.Rows[i].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[i].Name + " 單價:" + MerchandiseList[i].Price + " 入庫數量: " + "商品類型ID:" + MerchandiseList[i].TID + " 金額: " +
MerchandiseList[i].Amount + " 備注:" + MerchandiseList[i].Remark + "<br>");
}
}
頁面代碼
<div>
<fieldset style="width: 80%; text-align: center;">
<legend>
<asp:Label ID="lblMessage" runat="server" Text="附件資訊"></asp:Label>
</legend>
<table 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" BorderWidth="0" Text="0" 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>
</table>
<asp:Table ID="Table1" runat="server" BorderColor="black" BorderWidth="1" CellPadding="0"
CellSpacing="0" Width="80%" EnableViewState="False">
</asp:Table>
</fieldset>
<table 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>
效果:
