网友留言“请问 我想做一个这效果,当我输入数字的时候,它动态的创建行数,是怎么样实现!”,原问题来自http://www.cnblogs.com/insus/archive/2011/11/17/2252372.html#2256524
时间关系,一直没得闲下来及时帮上他的忙,现把实现过程帖出来。另外网友的提供的问题过于简单,也不一定能真正符合他的要求。首先看看效果。
Insus.NET把动态产生表格,写在一个UserControl用户控件,把它拉到页面上去即可。


protected
void ButtonSetup_Click(
object sender, EventArgs e)
{
if ( string.IsNullOrWhiteSpace( this.TextBox1.Text)) return;
try
{
this.TableUploadlayout.Visible = true;
GenerateTable(Convert.ToInt32( this.TextBox1.Text.Trim()));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
private void GenerateTable( int rows)
{
int tableRows = rows; // 接收用户设置行数
int tableCells = 4; // 4列
int tbId = 1; // 由于一行有多个Textbox,要设置每个TextBox的ID唯一性。
for ( int i = 1; i <= tableRows; i++)
{
TableRow tableRow = new TableRow();
for ( int j = 1; j <= tableCells; j++)
{
switch (j)
{
case 1: // 第一列
TableCell tableCell1 = new TableCell();
tableCell1.BorderWidth = Unit.Pixel( 1);
FileUpload fileUpload = new FileUpload();
fileUpload.ClientIDMode = ClientIDMode.Static;
fileUpload.ID = " FileUpload " + i.ToString();
tableCell1.Controls.Add(fileUpload);
tableRow.Cells.Add(tableCell1);
break;
case 2: // 第二列
TableCell tableCell2 = new TableCell();
tableCell2.BorderWidth = Unit.Pixel( 1);
TextBox textBox1 = new TextBox();
textBox1.ClientIDMode = ClientIDMode.Static;
textBox1.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell2.Controls.Add(textBox1);
tableRow.Cells.Add(tableCell2);
break;
case 3: // 第三列
TableCell tableCell3 = new TableCell();
tableCell3.BorderWidth = Unit.Pixel( 1);
TextBox textBox2 = new TextBox();
textBox2.ClientIDMode = ClientIDMode.Static;
textBox2.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell3.Controls.Add(textBox2);
tableRow.Cells.Add(tableCell3);
break;
case 4: // 第四列
TableCell tableCell4 = new TableCell();
tableCell4.BorderWidth = Unit.Pixel( 1);
DropDownList dropDownList = new DropDownList();
dropDownList.ClientIDMode = ClientIDMode.Static;
dropDownList.ID = " DropDownList " + i.ToString();
dropDownList.DataSource = PhotoAlbum();
dropDownList.DataBind();
tableCell4.Controls.Add(dropDownList);
tableRow.Cells.Add(tableCell4);
break;
}
}
TableUploadlayout.Rows.Add(tableRow);
}
}
private List< string> PhotoAlbum()
{
List< string> pa = new List< string>();
pa.Add( " 长城 ");
pa.Add( " 北京 ");
pa.Add( " 桂林 ");
return pa;
}
{
if ( string.IsNullOrWhiteSpace( this.TextBox1.Text)) return;
try
{
this.TableUploadlayout.Visible = true;
GenerateTable(Convert.ToInt32( this.TextBox1.Text.Trim()));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
private void GenerateTable( int rows)
{
int tableRows = rows; // 接收用户设置行数
int tableCells = 4; // 4列
int tbId = 1; // 由于一行有多个Textbox,要设置每个TextBox的ID唯一性。
for ( int i = 1; i <= tableRows; i++)
{
TableRow tableRow = new TableRow();
for ( int j = 1; j <= tableCells; j++)
{
switch (j)
{
case 1: // 第一列
TableCell tableCell1 = new TableCell();
tableCell1.BorderWidth = Unit.Pixel( 1);
FileUpload fileUpload = new FileUpload();
fileUpload.ClientIDMode = ClientIDMode.Static;
fileUpload.ID = " FileUpload " + i.ToString();
tableCell1.Controls.Add(fileUpload);
tableRow.Cells.Add(tableCell1);
break;
case 2: // 第二列
TableCell tableCell2 = new TableCell();
tableCell2.BorderWidth = Unit.Pixel( 1);
TextBox textBox1 = new TextBox();
textBox1.ClientIDMode = ClientIDMode.Static;
textBox1.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell2.Controls.Add(textBox1);
tableRow.Cells.Add(tableCell2);
break;
case 3: // 第三列
TableCell tableCell3 = new TableCell();
tableCell3.BorderWidth = Unit.Pixel( 1);
TextBox textBox2 = new TextBox();
textBox2.ClientIDMode = ClientIDMode.Static;
textBox2.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell3.Controls.Add(textBox2);
tableRow.Cells.Add(tableCell3);
break;
case 4: // 第四列
TableCell tableCell4 = new TableCell();
tableCell4.BorderWidth = Unit.Pixel( 1);
DropDownList dropDownList = new DropDownList();
dropDownList.ClientIDMode = ClientIDMode.Static;
dropDownList.ID = " DropDownList " + i.ToString();
dropDownList.DataSource = PhotoAlbum();
dropDownList.DataBind();
tableCell4.Controls.Add(dropDownList);
tableRow.Cells.Add(tableCell4);
break;
}
}
TableUploadlayout.Rows.Add(tableRow);
}
}
private List< string> PhotoAlbum()
{
List< string> pa = new List< string>();
pa.Add( " 长城 ");
pa.Add( " 北京 ");
pa.Add( " 桂林 ");
return pa;
}
其实第二列与第三列写为一个case:


case
2:
//
第二列
case 3: // 第三列
TableCell tableCell = new TableCell();
tableCell.BorderWidth = Unit.Pixel( 1);
TextBox textBox = new TextBox();
textBox.ClientIDMode = ClientIDMode.Static;
textBox.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell.Controls.Add(textBox);
tableRow.Cells.Add(tableCell);
break;
case 3: // 第三列
TableCell tableCell = new TableCell();
tableCell.BorderWidth = Unit.Pixel( 1);
TextBox textBox = new TextBox();
textBox.ClientIDMode = ClientIDMode.Static;
textBox.ID = " TextBox " + tbId.ToString();
tbId = tbId + 1;
tableCell.Controls.Add(textBox);
tableRow.Cells.Add(tableCell);
break;
源代码打包在下链接,可以下载来看即可。
http://download.cnblogs.com/insus/ASPDOTNET/DymanicallyGenerateMulitRows.rar