asp.net动态表格生成(收藏)
<%@ 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中
- }
- }
- 就是动态来添加table的操作