见天讲了几个以前学过的功能!
主要的就是动态生成table!下面我把代码给大家分享一下!
1, 动态生成Htmltable!首先要引入命名空间!
using System.Web.UI.HtmlControls;
protected void Button1_Click(object sender, EventArgs e)
{
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);
}
table.Rows.Add(row);
}
this.Controls.Add(table);
}
2;动态生成Web Table
命名空间:using System.Web.UI.WebControls;
protected void Button2_Click(object sender, EventArgs e)
{
Table table = new Table();
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 3; j++)
{
TableCell cell = new TableCell();
Button btn = new Button();
btn.Text = i.ToString() + j.ToString();
cell.Controls.Add(btn);
row.Controls.Add(cell);
}
table.Controls.Add(row);
}
this.Panel1.Controls.Add(table);
}
2, 动态生成 DataTable
命名空间; using System.Data;
protected void Button3_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "id";
dc.DataType=typeof(int); //Type.GetType("System.Int32");
dc.AllowDBNull = false;
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep=1;
table.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "name";
dc.DataType=typeof(string);
dc.AllowDBNull = false;
table.Columns.Add(dc);
DataRow ro = table.NewRow();
ro["name"] = "zhangsan";
table.Rows.Add(ro);
this.DropDownList1.DataSource = table;
//this.DropDownList1.DataTextField = "id";
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataBind();
}