我创建的窗体入下:
TextBox tb = new TextBox();//textbox
Label mylabel = new Label();//title
Label mylabel2=new Label(); //plan
DropDownList dd=new DropDownList();
dd.Attributes["algin"]="right";
Label mylabel1 = new Label();//<br> #region
tb.Width=504;
tb.Height=191;
tb.TextMode=TextBoxMode.MultiLine;
dd.Items.Add("0%");
dd.Items.Add("10%");
dd.Items.Add("20%");
dd.Items.Add("30%");
dd.Items.Add("40%");
dd.Items.Add("50%");
dd.Items.Add("60%");
dd.Items.Add("70%");
dd.Items.Add("80%");
dd.Items.Add("90%");
dd.Items.Add("100%");
#endregion TableRow row1 = new TableRow();
TableRow row2=new TableRow();
TableCell cell1 = new TableCell();
cell1.Attributes["algin"]="left";
TableCell cell2=new TableCell();
TableCell cell3 = new TableCell();
cell3.Attributes["align"] = "right";
mylabel2.Text="进度率 : ";
mylabel.Text=drchange["S_Title"].ToString();
tb.Text=drchange["S_Con"].ToString();
cell1.Controls.Add(mylabel);
cell2.Controls.Add(tb);
cell2.Attributes["colspan"] = "2";
cell3.Controls.Add(mylabel2);
cell3.Controls.Add(dd); row1.Cells.Add(cell1);
row1.Cells.Add(cell3);
row2.Cells.Add(cell2); this.Table1.Rows.Add(row1);
this.Table1.Rows.Add(row2);
TextBox tb = new TextBox();//textbox
Label mylabel = new Label();//title
Label mylabel2=new Label(); //plan
DropDownList dd=new DropDownList();
dd.Attributes["algin"]="right";
Label mylabel1 = new Label();//<br> #region
tb.Width=504;
tb.Height=191;
tb.TextMode=TextBoxMode.MultiLine;
dd.Items.Add("0%");
dd.Items.Add("10%");
dd.Items.Add("20%");
dd.Items.Add("30%");
dd.Items.Add("40%");
dd.Items.Add("50%");
dd.Items.Add("60%");
dd.Items.Add("70%");
dd.Items.Add("80%");
dd.Items.Add("90%");
dd.Items.Add("100%");
#endregion TableRow row1 = new TableRow();
TableRow row2=new TableRow();
TableCell cell1 = new TableCell();
cell1.Attributes["algin"]="left";
TableCell cell2=new TableCell();
TableCell cell3 = new TableCell();
cell3.Attributes["align"] = "right";
mylabel2.Text="进度率 : ";
mylabel.Text=drchange["S_Title"].ToString();
tb.Text=drchange["S_Con"].ToString();
cell1.Controls.Add(mylabel);
cell2.Controls.Add(tb);
cell2.Attributes["colspan"] = "2";
cell3.Controls.Add(mylabel2);
cell3.Controls.Add(dd); row1.Cells.Add(cell1);
row1.Cells.Add(cell3);
row2.Cells.Add(cell2); this.Table1.Rows.Add(row1);
this.Table1.Rows.Add(row2);
http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx?mfr=true
connchange.Open();
for(int i=0;i<Table1.Rows.Count;i++)
{
Label mylabel =(Label)Table1.Rows[i].Cells[0].Controls[0];
DropDownList dd=(DropDownList)Table1.Rows[i].Cells[0].Controls[3];
TextBox tb=(TextBox)Table1.Rows[i].Cells[0].Controls[3];
SqlCommand cmd = new SqlCommand("insert into DAY_REPORT_SUB (D_S_Title_SUB,D_Info_SUB,D_Plan_SUB) values ('"+mylabel.Text.ToString()+"','"+tb.Text.ToString()+"','"+dd.SelectedValue.ToString()+"')",connchange);
cmd.ExecuteNonQuery();
}
connchange.Close();
if (ViewState["DataFieldCount"] != null)
{
ArrayList bb = new ArrayList();//页面上动态添加控件的id foreach (System.Web.UI.Control t in Panel1.Controls)
{
if (t.GetType().ToString() == "System.Web.UI.WebControls.Button")
{
System.Web.UI.WebControls.Button tb = (Button)t;
string numcount = tb.ID.ToString().TrimEnd().Substring(6);
bb.Add(numcount);
}
}
for (int k = 0; k < bb.Count; k++)
{
string selectitem = "";
string selrmtyp1 = "";
string selrmtypcd1 = "";
string selrmamt1 = "";
string selrmcount1 = "";
string newresv1 = "";
string arrtext = "";
string leatext = "";
string numtext = "";
string pertext = "";
foreach (System.Web.UI.Control groupcount in Panel1.Controls)
{
if (groupcount.GetType().ToString() == "System.Web.UI.WebControls.DropDownList" || groupcount.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
int i = groupcount.ID.ToString().TrimEnd().Length - 1; if (groupcount.ID.ToString().TrimEnd().Substring(i) == bb[k].ToString())
{
if (groupcount.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
{
System.Web.UI.WebControls.DropDownList rm_typ = (DropDownList)groupcount;
selectitem = rm_typ.SelectedValue;
selrmtyp1 = selectitem.Substring(0, selectitem.LastIndexOf("("));//房类代码
selrmtypcd1 = getrmtypcd(DBConnection, selrmtyp1, htlcd);
selrmamt1 = selectitem.Substring(selectitem.IndexOf("$") + 1, selectitem.LastIndexOf(",") - selectitem.IndexOf("$") - 1);//房价
selrmcount1 = selectitem.Substring(selectitem.IndexOf(",") + 1, selectitem.LastIndexOf("间") - selectitem.IndexOf(",") - 1);//可用房
}
if (groupcount.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
System.Web.UI.WebControls.TextBox tb = (TextBox)groupcount;
if (tb.ID == "arr_dt" + bb[k].ToString())
{
arrtext = tb.Text;
}
if (tb.ID == "lea_dt" + bb[k].ToString())
{
leatext = tb.Text;
}
if (tb.ID == "rm_num" + bb[k].ToString())
{
numtext = tb.Text.ToString();
}
if (tb.ID == "pre_num" + bb[k].ToString())
{
pertext = tb.Text;
} }
}
}
你看看吧,就是这么个思路,我用命名1234,比较好搞一点,呵呵
for(int i=0;i<Table1.Rows.Count;i++)
{
Label mylabel =(Label)Table1.Rows[i].Cells[0].Controls[0];
DropDownList dd=(DropDownList)Table1.Rows[i].Cells[0].Controls[3];
TextBox tb=(TextBox)Table1.Rows[i].Cells[0].Controls[3];
SqlCommand cmd = new SqlCommand("insert into DAY_REPORT_SUB (D_S_Title_SUB,D_Info_SUB,D_Plan_SUB) values ('"+mylabel.Text.ToString()+"','"+tb.Text.ToString()+"','"+dd.SelectedValue.ToString()+"')",connchange);
connchange.Open();
cmd.ExecuteNonQuery();
connchange.Close();
}也可以一直拼接SQL语句,只执行
看看cmd里面的CommandText是什么,直接拿到Sql上运行下。。你也不把错误跟我们说下?
Label mylabel =(Label)Table1.Rows[i].Cells[0].Controls[0];可以运行过去
DropDownList dd=(DropDownList)Table1.Rows[i].Cells[0].Controls[3];
就运行不过去了
说"Specified argument was out of the range of valid values. Parameter name: index "
超出范围... 我怀疑是 Rows Cells Controls 跟的参数不对 或者是在table的不对
{
Label mylabel =(Label)Table1.Rows[i].Cells[0].Controls[0];
DropDownList dd=(DropDownList)Table1.Rows[i].Cells[1].Controls[1];
i=++i;
TextBox tb=(TextBox)Table1.Rows[i].Cells[0].Controls[0];
SqlCommand cmd = new SqlCommand("insert into DAY_REPORT_SUB (D_S_Title_SUB,D_Plan_SUB,D_Info_SUB) values ('"+mylabel.Text.ToString()+"','"+dd.SelectedValue.ToString()+"','"+tb.Text.ToString()+"')",connchange);
cmd.Connection=connchange;
connchange.Open();
cmd.ExecuteNonQuery();
connchange.Close();
}
靠人不如靠自己 ! ! !