如何在c#中动态创建表
Record 1 1 2 3 a b c m n o
我必须在c#中动态创建上表我正在尝试但没有得到
protected void Page_Load(object sender, EventArgs e) { HtmlTableRow row = null; HtmlTableCell cell = null; for(int i = 0; i < 5; i++) { row = new HtmlTableRow(); cell = new HtmlTableCell(); tableContent.Controls.AddAt(i, row); row.Controls.AddAt(i, cell); cell.InnerText="1"; } }
您可以尝试使用此代码来创建表。
首先将此标记放在您的aspx页面中
然后在Page_Load中尝试此代码
protected void Page_Load(object sender, EventArgs e) { HtmlTableRow row = new HtmlTableRow(); HtmlTableCell cell = new HtmlTableCell(); cell.ColSpan =3; cell.InnerText = "Record 1"; row.Cells.Add(cell); tableContent.Rows.Add(row); row = new HtmlTableRow(); cell = new HtmlTableCell(); cell.InnerText = "1"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "2"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "3"; row.Cells.Add(cell); tableContent.Rows.Add(row); row = new HtmlTableRow(); cell = new HtmlTableCell(); cell.InnerText = "a"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "b"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "c"; row.Cells.Add(cell); tableContent.Rows.Add(row); row = new HtmlTableRow(); cell = new HtmlTableCell(); cell.InnerText = "m"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "n"; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = "o"; row.Cells.Add(cell); tableContent.Rows.Add(row); row = new HtmlTableRow(); cell = new HtmlTableCell(); HtmlInputButton input = new HtmlInputButton(); input.ID = "Button1"; input.Value = "button"; cell.ColSpan = 3; cell.Controls.Add(input); row.Cells.Add(cell); tableContent.Rows.Add(row); }
或者您可以通过将单元格值存储在2D数组中来尝试此操作
protected void Page_Load(object sender, EventArgs e) { String[,] cellValues = { { "1", "2", "3" }, { "a", "b", "c" }, { "m", "n", "o" } }; HtmlTableRow row = new HtmlTableRow(); HtmlTableCell cell = new HtmlTableCell(); cell.ColSpan = 3; cell.InnerText = "Record 1"; row.Cells.Add(cell); tableContent.Rows.Add(row); for (int i = 0; i < cellValues.GetLength(0); i++) { row = new HtmlTableRow(); for (int j = 0; j < cellValues.GetLength(1); j++) { cell = new HtmlTableCell(); cell.InnerText = cellValues[i, j]; row.Cells.Add(cell); } tableContent.Rows.Add(row); } row = new HtmlTableRow(); cell = new HtmlTableCell(); HtmlInputButton input = new HtmlInputButton(); input.ID = "Button1"; input.Value = "button"; cell.ColSpan = 3; cell.Controls.Add(input); row.Cells.Add(cell); tableContent.Rows.Add(row); }
我用这段代码在C#中动态生成表。
string connectString = ConfigurationManager.ConnectionStrings["Sample4ConnectionString"].ToString(); StudentsModelDataContext db = new StudentsModelDataContext(connectString); var studentList = db.Students; Table tb = new Table(); tb.BorderWidth = 3; tb.BorderStyle = BorderStyle.Solid; tb.ID = "myTable"; foreach (Student student in studentList) { TableRow tr = new TableRow(); TableCell tc1 = new TableCell(); TableCell tc2 = new TableCell(); TableCell tc3 = new TableCell(); TableCell tc4 = new TableCell(); TableCell tc5 = new TableCell(); tc1.Text = student.Name; tc1.BorderWidth = 2; tr.Cells.Add(tc1); tc2.Text = student.Email; tc2.BorderWidth = 2; tr.Cells.Add(tc2); tc3.Text = student.Gender; tc3.BorderWidth = 2; tr.Cells.Add(tc3); tc4.Text = student.BirthDate.ToString(); tc4.BorderWidth = 2; tr.Cells.Add(tc4); tc5.Text = student.TotalMarks.ToString(); tc5.BorderWidth = 2; tr.Cells.Add(tc5); tb.Rows.Add(tr); } form1.Controls.Add(tb);
这生成了以下图像。
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim rows As Integer = TextBox1.Text Dim columns As Integer = TextBox2.Text Dim r As Integer Dim c As Integer Literal1.Text = "" For r = 1 To rows Literal1.Text &= "" For c = 1 To columns Literal1.Text &= "" Literal1.Text &= " " Literal1.Text &= " " Next c Literal1.Text &= " " Next r Literal1.Text &= "
" End Sub
- 在升级到VS 2010 / .NET 4.0后,Membership.ValidateUser始终返回false
- 我的Entity Framework存储库和服务层方法应返回哪些类型:List,IEnumerable,IQueryable?
- 从类库中访问App.Config设置通过unit testing项目调用
- 如何检查控件是否是另一个控件的子控件? “Control.IsChildOf”
- 使用ASP.NET获取Active Directory信息,无需用户名和密码
- gridview将下拉列表绑定到List <keyvaluePair >
- entity framework种子不起作用
- 有jQuery倒计时的问题? functionserverSync:serverTime
- 使用ASP.NET Web API,我的ExecutionContext没有流入异步操作