创建动态表格:
HtmlTable table = new HtmlTable(); //表格创建完成
for (int i = 0; i < 5; i++) //控制行
{
HtmlTableRow row = new HtmlTableRow();
for (int j = 0; j < 5; j++) //控制单元格
{
HtmlTableCell cell= new HtmlTableCell();
//cell.InnerHtml = string.Format("i:{0};j:{1}", i, j);
row.Cells.Add(cell);
}
生成的htmltable
Table t = new Table();
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 2; j++)
{
TableCell cell = new TableCell();
Button btn = new Button();
btn.Text = i.ToString() + j.ToString();
cell.Controls.Add(btn);
row.Cells.Add(cell);
}
t.Rows.Add(row);
}
this.Panel1.Controls.Add(t);
生成web服务器控件table
人民币转换:
public static string ToChineseNumber<T>(T num)
使用泛型
string[] chinesePos = new string[] { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" };
string[] chinese = new string[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
定义两个数组分别用来取位和转换成大写
string strNum = num.ToString(); //要转换数字的字符串形式
string oneNum = ""; //用来存储一个数字
StringBuilder sb = new StringBuilder(); //用来存储转换之后的结果
int dotPos = strNum.IndexOf(".");
string integer = ""; //整数部分
if (dotPos != -1)
integer = strNum.Substring(0, dotPos);
else
integer = strNum;
使用interger保存整数部分
while (integer.Length > 0)
{ //转换整数部分
int len = integer.Length; //数字长度
oneNum = integer.Substring(0, 1); //取高位数字
integer = integer.Substring(1); //取出剩余数字,删掉高位数字
sb.Append(chinese[int.Parse(oneNum)]);//添加高位数字对应的大写
sb.Append(chinesePos[len - 1]); //添加数字对应的位权
}
整数部分转换完成,转换小数
if (dotPos != -1)
{ //转换小数部分
string little = strNum.Substring(dotPos + 1);
if (little.Length > 1)
{
sb.Append(chinese[int.Parse(little.Substring(0, 1))]).Append("角"); //数字
sb.Append(chinese[int.Parse(little.Substring(1, 1))]).Append("分"); //数字
}
}
return sb.ToString();
}