下面是Page_Load()事件里面的部分代码:
SqlConnection conn = new SqlConnection(txtSqlConn.Text);
SqlDataAdapter da = new SqlDataAdapter("select top 1 * from (" + txtSelect.Text + ") t1", conn);
try
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "InfoTable");
Table tb = new Table();//创建一个表格
tb.BorderWidth = Unit.Parse("0");
tb.Width = Unit.Parse("100%");
for (int i = 0; ds.Tables[0].Columns.Count > i; i++)
{
if (ds.Tables["InfoTable"].Columns[i].DataType.ToString() == "System.Int32")
{
TableRow tr = new TableRow();//创建一行
TableCell cell1 = new TableCell();//创建单元格,也就是第一列
Label lbl = new Label();
lbl.ID = "lblName" + (int.Parse(ViewState["columns"].ToString()) + 1);
lbl.Text = ds.Tables["InfoTable"].Columns[i].ColumnName;
cell1.Controls.Add(lbl);
tr.Cells.Add(cell1);//添加到行中
TableCell cell2 = new TableCell();//创建第二列
DropDownList List = new DropDownList();
List.ID = "List" + (int.Parse(ViewState["columns"].ToString()) + 1);
//List.AutoPostBack = true;
List.Items.Add("无 ");
List.Items.Add("求和");
List.Items.Add("最大值");
List.Items.Add("最小值");
List.Items.Add("平均数");
cell2.Controls.Add(List);
tr.Cells.Add(cell2);
tb.Rows.Add(tr);//添加到表格中
ViewState["columns"] = int.Parse(ViewState["columns"].ToString()) + 1;
}
}
PlaceHolder1.Controls.Add(tb);
}
catch (Exception ex)
{
ErrorInfo.Text = ex.ToString();
}
finally
{
conn.Close();
}
SqlDataAdapter da = new SqlDataAdapter("select top 1 * from (" + txtSelect.Text + ") t1", conn);
try
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "InfoTable");
Table tb = new Table();//创建一个表格
tb.BorderWidth = Unit.Parse("0");
tb.Width = Unit.Parse("100%");
for (int i = 0; ds.Tables[0].Columns.Count > i; i++)
{
if (ds.Tables["InfoTable"].Columns[i].DataType.ToString() == "System.Int32")
{
TableRow tr = new TableRow();//创建一行
TableCell cell1 = new TableCell();//创建单元格,也就是第一列
Label lbl = new Label();
lbl.ID = "lblName" + (int.Parse(ViewState["columns"].ToString()) + 1);
lbl.Text = ds.Tables["InfoTable"].Columns[i].ColumnName;
cell1.Controls.Add(lbl);
tr.Cells.Add(cell1);//添加到行中
TableCell cell2 = new TableCell();//创建第二列
DropDownList List = new DropDownList();
List.ID = "List" + (int.Parse(ViewState["columns"].ToString()) + 1);
//List.AutoPostBack = true;
List.Items.Add("无 ");
List.Items.Add("求和");
List.Items.Add("最大值");
List.Items.Add("最小值");
List.Items.Add("平均数");
cell2.Controls.Add(List);
tr.Cells.Add(cell2);
tb.Rows.Add(tr);//添加到表格中
ViewState["columns"] = int.Parse(ViewState["columns"].ToString()) + 1;
}
}
PlaceHolder1.Controls.Add(tb);
}
catch (Exception ex)
{
ErrorInfo.Text = ex.ToString();
}
finally
{
conn.Close();
}
下面是按钮事件里取控件值:
string info = "";
for (int i = 0; i < int.Parse(ViewState["columns"].ToString()); i++)
{
Label lbl = (Label)PlaceHolder1.FindControl("lblName" + (i + 1));
DropDownList list = (DropDownList)PlaceHolder1.FindControl("List" + (i + 1));
info += lbl.Text + "," + list.SelectedItem.Text + ";";
}
for (int i = 0; i < int.Parse(ViewState["columns"].ToString()); i++)
{
Label lbl = (Label)PlaceHolder1.FindControl("lblName" + (i + 1));
DropDownList list = (DropDownList)PlaceHolder1.FindControl("List" + (i + 1));
info += lbl.Text + "," + list.SelectedItem.Text + ";";
}