如何在asp.net中的代码隐藏中动态创建表 [英] How to create table dynamically in codebehind in asp.net

查看:73
本文介绍了如何在asp.net中的代码隐藏中动态创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在代码隐藏中动态创建表,并且表数据来自数据库.以及如何为该表提供colspan和rowspan(如果两列中的一列数据相同,那么我要合并)


谢谢,

How to create table dynamically in codebehind and table data come from database. and How to give colspan and rowspan for that table(if one column data is same in 2 rows then i want merge)


Thanks,

推荐答案

朋友...

请查看以下代码.
表tb = new Table();

tb.BorderWidth = Unit.Pixel(5);

for(int i = 1; i< = Convert.ToInt32(TextBox1.Text); i ++)

{

TableRow tr =新的TableRow();

for(int j = 1; j< = 3; j ++)

{

TableCell td = new TableCell();

TextBox c_text = new TextBox();

c_text.Visible = true;

c_text.Text ="R" + i.ToString()+ j.ToString();

c_text.ID ="R" + i.ToString()+ j.ToString();

td.Controls.Add(c_text);

tr.Cells.Add(td);

tb.Rows.Add(tr);

form1.Controls.Add(tb);

}

}



可能对您有帮助...
Hi Friend...

Please look into the below codes.
Table tb = new Table();

tb.BorderWidth = Unit.Pixel(5);

for (int i = 1; i <= Convert.ToInt32(TextBox1.Text ); i++)

{

TableRow tr = new TableRow();

for (int j = 1; j <= 3; j++)

{

TableCell td = new TableCell();

TextBox c_text = new TextBox();

c_text.Visible = true;

c_text.Text = "R"+i.ToString()+j.ToString();

c_text.ID = "R" + i.ToString() + j.ToString();

td.Controls.Add(c_text);

tr.Cells.Add(td);

tb.Rows.Add(tr);

form1.Controls.Add(tb);

}

}



It may be help for u ...


您好

我在这里发布一些示例代码,以便对其进行一次检查

Hi

Here I''m posting some sample code for that check it once

 string tablestring="";
//dt is datatable object which holds DB results.
 tablestring=tablestring+"<table width="100%"><tr><td>First Column Heading</td><td>Second column Heading</td><td>Third column Heading</td></tr>";
 foreach(datarow dr in dt.rows)
{
   tablestring=tablestring+"<tr><td>"+dr[0].tostring()+"</td><td>"+dr[1].tostring()+"</td><td>"+dr[2].tostring()+"</td></tr>";
}
tablestring=tablestring+"</table>";

divTable.innerHTML=tablestring;



我希望你能理解我的所作所为.

最好的



I hope you understood what I did.

All the Best


如果您要谈论创建HTML表,则可以使用HtmlTextWriter类写出HTML.不过,更好的选择是使用标准的DataGrid,该标准允许您仅通过绑定将内容写成表格式,就可以节省很多工作.
If you are talking about creating a HTML table, then you would use the HtmlTextWriter class to write out HTML. A better bet, though, would be to use a standard DataGrid which allows you to write out the content as table format, just by binding to it, and saves you a lot of work.


这篇关于如何在asp.net中的代码隐藏中动态创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆