asp.net动态表格生成(收藏)
2009-02-17 20:50
411 查看
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<mce:script runat="server"><!--
void Page_Load(Object sender, EventArgs e) {
int numrows = int.Parse(DropDown1.SelectedItem.Value);///为啥用int.parse?
int numcells = int.Parse(DropDown2.SelectedItem.Value);
for (int j = 0; j < numrows; j++) {
TableRow r = new TableRow();
for (int i = 0; i < numcells; i++)
{
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString()));///这句话啥意思?c.controls.add 和 new literalControl是啥意思?
///
r.Cells.Add(c);///这句话啥意思啊?
}
Table1.Rows.Add(r);///这句话啥意思啊?
}
}
// --></mce:script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Table,TableRow,tablecell控件演示</title>
</head>
<body>
<h3><font face="宋体">表的示例</font></h3>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" Font-Names="宋体" Font-Size="9pt" CellPadding="5" CellSpacing="0" BorderColor="black" BorderWidth="1" GridLines="Both" runat="server" />
<p>
表行:
<asp:DropDownList ID="DropDown1" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
表列
<asp:DropDownList ID="DropDown2" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
</p>
<asp:Button Text="创建表" runat="server" ID="Button1" />
</div>
</form>
</body>
</html>
我的问题都在上面,大大们给的建议
DropDown1.SelectedItem.Value为string型,要转化为int
void Page_Load(Object sender, EventArgs e) {
int numrows = int.Parse(DropDown1.SelectedItem.Value);///将DropDown1值转换为int型
int numcells = int.Parse(DropDown2.SelectedItem.Value);
for (int j = 0; j < numrows; j++) {
TableRow r = new TableRow();
for (int i = 0; i < numcells; i++)
{
TableCell c = new TableCell();
//TableCell 为单元格 c.Controls.Add 意思是在单元格中添加控件,new LiteralControl 创建了LiteralControl 控件
//显示第几行第几列
c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString
r.Cells.Add(c); //将Tablecell添加到table的row中
}
Table1.Rows.Add(r);// 将行添加到table中
}
}
[b]就是动态来添加table的操作 [/b]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<mce:script runat="server"><!--
void Page_Load(Object sender, EventArgs e) {
int numrows = int.Parse(DropDown1.SelectedItem.Value);///为啥用int.parse?
int numcells = int.Parse(DropDown2.SelectedItem.Value);
for (int j = 0; j < numrows; j++) {
TableRow r = new TableRow();
for (int i = 0; i < numcells; i++)
{
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString()));///这句话啥意思?c.controls.add 和 new literalControl是啥意思?
///
r.Cells.Add(c);///这句话啥意思啊?
}
Table1.Rows.Add(r);///这句话啥意思啊?
}
}
// --></mce:script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Table,TableRow,tablecell控件演示</title>
</head>
<body>
<h3><font face="宋体">表的示例</font></h3>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" Font-Names="宋体" Font-Size="9pt" CellPadding="5" CellSpacing="0" BorderColor="black" BorderWidth="1" GridLines="Both" runat="server" />
<p>
表行:
<asp:DropDownList ID="DropDown1" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
表列
<asp:DropDownList ID="DropDown2" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
</p>
<asp:Button Text="创建表" runat="server" ID="Button1" />
</div>
</form>
</body>
</html>
我的问题都在上面,大大们给的建议
DropDown1.SelectedItem.Value为string型,要转化为int
void Page_Load(Object sender, EventArgs e) {
int numrows = int.Parse(DropDown1.SelectedItem.Value);///将DropDown1值转换为int型
int numcells = int.Parse(DropDown2.SelectedItem.Value);
for (int j = 0; j < numrows; j++) {
TableRow r = new TableRow();
for (int i = 0; i < numcells; i++)
{
TableCell c = new TableCell();
//TableCell 为单元格 c.Controls.Add 意思是在单元格中添加控件,new LiteralControl 创建了LiteralControl 控件
//显示第几行第几列
c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString
r.Cells.Add(c); //将Tablecell添加到table的row中
}
Table1.Rows.Add(r);// 将行添加到table中
}
}
[b]就是动态来添加table的操作 [/b]
相关文章推荐
- asp.net 动态生成表格
- asp.net JQuery Ajax WebService 搜索 自动匹配 动态生成表格 点击表格 获取表格内容
- asp.net动态生成表格和控件
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法
- asp.net下动态生成不规则表格
- 利用UrlRewrite,asp.net动态生成htm页面【收藏】
- asp.net在页面中动态生成表格
- ASP.NET中服务器控件Table动态生成表格及其属性介绍
- asp.net在表格中动态生成一个可以链接到另外一个页面的图片
- Asp.Net Table控件动态生成表格操作实例(代码调试通过)
- Asp.net(C#)动态生成表格中的按钮和事件响应
- ASP.net中动态生成表格/文本框以及获取文本框内容的方法
- asp.net 动态表格生成1.
- asp.net 生成word 动态的填充表格(Microsoft的自带组件)
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法(转)
- asp.net 动态生成表格
- asp.net下动态生成不规则表格
- ASP.NET中应用Excel:(6)在服务器端生成HTML表格