public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ConStr = @"Server=(local)\sqlexpress;DataBase=db_07;Uid=sa;Pwd=a963.";
string cmdtxt = "SELECT * FROM tb_07";
SqlConnection Con = new SqlConnection(ConStr);
SqlDataAdapter da = new SqlDataAdapter(cmdtxt, Con);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
if (ViewState["Count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
{
//AddTextBox();
Response.Write("<script>allert('ViewState!=null 当!当!当!当!');</script>");
}
}
else
{
this.btnData.Enabled = false;
Response.Write("<script>allert('ViewState==null 哈哈!');</script>");
}
}
//向页面中添加文本框
public void AddTextBox()
{
//行
TableRow tr = new TableRow();
//单元格1
TableCell tc1 = new TableCell();
//TextBox
TextBox txt = new TextBox();
txt.ID = "tb" + Table1.Rows.Count;
txt.Font.Size = FontUnit.Point(9);
//单元格2
TableCell tc2 = new TableCell();
//Label
Label lab = new Label();
lab.ID = "lab" + Table1.Rows.Count;
lab.Width = 50;
lab.Text = "数据" + (Table1.Rows.Count + 1);
tc2.Controls.Add(lab);
tc1.Controls.Add(txt);
tr.Cells.Add(tc2);
tr.Cells.Add(tc1);
Table1.Rows.Add(tr);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>allert('添加输入框');</script>");
//动态添加控件
AddTextBox();
ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
this.btnData.Enabled = true;
}
protected void btnData_Click(object sender, EventArgs e)
{
Response.Write("<script>allert('开始插入');</script>");
string ConStr = @"Server=(local)\sqlexpress;DataBase=db_07;Uid=sa;Pwd=a963.";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Com;
string cmdtxt = String.Empty;
//将文本框中的内容循环插入到数据库中
for (int i = 0; i < Table1.Rows.Count; i++)
{
cmdtxt = "INSERT INTO tb_07(name) VALUES('" + ((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + "')";
Com = new SqlCommand(cmdtxt, Con);
Com.ExecuteNonQuery();
}
Response.Write("<script>allert('数据插入成功!');location='Default.aspx'</script>");
}
}
在上面,为什么在Page_Load里面,要再次执行 AddTextBox();呢,
如果不执行,发现添加不了数据。
点击添加数据按钮后,不是直接读取输入框中的数据就行了,为什么还要再一次生成输入框?
我是新手,自学来着,请指点一下
因为动态生成的控件会在下一次Load的时候消失,因此必须在Load的时候把他加回去,其实是个很复杂的问题……
嗯,主要是页面模型的问题。很多人把asp.net当winform了,其实asp.net是无状态的。另外label和textbox貌似都可以用viewstate保存状态。
支持楼上的
让页面不回发一下,!ispostback
点击按钮之后,asp.net实际上会先执行PageLoad方法,然后才会进入click事件响应,如果PageLoad方法中没有填加控件,点击按钮后控件就会丢失。
如果你需要保存值,应该在LoadPage方法里将其写入数据库,或是用一个全局变量(在类内方法外声明)来保存。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
string ConStr = @"Server=(local)\sqlexpress;DataBase=db_07;Uid=sa;Pwd=a963.";
string cmdtxt = "SELECT * FROM tb_07";
SqlConnection Con = new SqlConnection(ConStr);
SqlDataAdapter da = new SqlDataAdapter(cmdtxt, Con);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
if (ViewState["Count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
{
//AddTextBox();
Response.Write("<script>allert('ViewState!=null 当!当!当!当!');</script>");
}
}
else
{
this.btnData.Enabled = false;
Response.Write("<script>allert('ViewState==null 哈哈!');</script>");
}
}
}
//向页面中添加文本框
public void AddTextBox()
{
//行
TableRow tr = new TableRow();
//单元格1
TableCell tc1 = new TableCell();
//TextBox
TextBox txt = new TextBox();
txt.ID = "tb" + Table1.Rows.Count;
txt.Font.Size = FontUnit.Point(9);
//单元格2
TableCell tc2 = new TableCell();
//Label
Label lab = new Label();
lab.ID = "lab" + Table1.Rows.Count;
lab.Width = 50;
lab.Text = "数据" + (Table1.Rows.Count + 1);
tc2.Controls.Add(lab);
tc1.Controls.Add(txt);
tr.Cells.Add(tc2);
tr.Cells.Add(tc1);
Table1.Rows.Add(tr);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>allert('添加输入框');</script>");
//动态添加控件
AddTextBox();
ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
this.btnData.Enabled = true;
}
protected void btnData_Click(object sender, EventArgs e)
{
Response.Write("<script>allert('开始插入');</script>");
string ConStr = @"Server=(local)\sqlexpress;DataBase=db_07;Uid=sa;Pwd=a963.";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Com;
string cmdtxt = String.Empty;
//将文本框中的内容循环插入到数据库中
for (int i = 0; i < Table1.Rows.Count; i++)
{
cmdtxt = "INSERT INTO tb_07(name) VALUES('" + ((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + "')";
Com = new SqlCommand(cmdtxt, Con);
Com.ExecuteNonQuery();
}
Response.Write("<script>allert('数据插入成功!');location='Default.aspx'</script>");
}
}
加上ispostback
ASp.NET得运行机制 是先Page_Load
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
AddTextBox();
}
}
public void AddTextBox()
{
。。。。。。。。
}
}