动态生成的DataGrid,点击里面的删除按钮,从第二次开始,要每点两次,才能执行删除操作。
大家看这前,最好看一下问题的由来:
http://community.csdn.net/Expert/topic/3965/3965047.xml?temp=.2934076,
因为,如果不用动态生成DataGrid,这段代码很好写,也不会出现什么问题。public class WebForm30 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateGrid(Panel1);
}
private void CreateGrid(Panel Panel1 )
{
DataGrid dg=new DataGrid();
ButtonColumn col=new ButtonColumn();
col.DataTextField="ISBN";
dg.ItemCommand+=new DataGridCommandEventHandler(dg_ItemCommand);
dg.Columns.Add(col);
dg.DataSource =this.GetDataSource();
dg.DataBind();
Panel1.Controls.Add(dg);
}
private DataSet GetDataSource()
{
string conn="server=(local);database=WroxBooks;uid=sa;pwd=;";
SqlDataAdapter da;
da=new SqlDataAdapter("select * from BookList",new SqlConnection(conn));
DataSet ds =new DataSet();
da.Fill(ds);
return ds;
}
private void dg_ItemCommand(object sender,DataGridCommandEventArgs e)
{
SqlConnection conn =new SqlConnection("server=(local);database=WroxBooks;uid=sa;pwd=123;");
SqlCommand com =new SqlCommand("delete from BookList where ISBN=@ISBN",conn);
com.Parameters.Add("@ISBN",((LinkButton)e.Item.Cells[0].Controls[0]).Text);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Panel Panel1;
Panel1=(Panel)e.Item.Page.FindControl("Panel1");
Panel1.Controls.Clear();
CreateGrid(Panel1);
}
}
主意:不能在页面的InitializeComponent()里对DataGrid声明,因为实际的情况是,我要生成若干个不同列数的DataGrid加到panel里面去。
因为各DataGrid的列数不尽相同,所以,对DataGrid的定义和数据绑定还要在一起。
大家看这前,最好看一下问题的由来:
http://community.csdn.net/Expert/topic/3965/3965047.xml?temp=.2934076,
因为,如果不用动态生成DataGrid,这段代码很好写,也不会出现什么问题。public class WebForm30 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateGrid(Panel1);
}
private void CreateGrid(Panel Panel1 )
{
DataGrid dg=new DataGrid();
ButtonColumn col=new ButtonColumn();
col.DataTextField="ISBN";
dg.ItemCommand+=new DataGridCommandEventHandler(dg_ItemCommand);
dg.Columns.Add(col);
dg.DataSource =this.GetDataSource();
dg.DataBind();
Panel1.Controls.Add(dg);
}
private DataSet GetDataSource()
{
string conn="server=(local);database=WroxBooks;uid=sa;pwd=;";
SqlDataAdapter da;
da=new SqlDataAdapter("select * from BookList",new SqlConnection(conn));
DataSet ds =new DataSet();
da.Fill(ds);
return ds;
}
private void dg_ItemCommand(object sender,DataGridCommandEventArgs e)
{
SqlConnection conn =new SqlConnection("server=(local);database=WroxBooks;uid=sa;pwd=123;");
SqlCommand com =new SqlCommand("delete from BookList where ISBN=@ISBN",conn);
com.Parameters.Add("@ISBN",((LinkButton)e.Item.Cells[0].Controls[0]).Text);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Panel Panel1;
Panel1=(Panel)e.Item.Page.FindControl("Panel1");
Panel1.Controls.Clear();
CreateGrid(Panel1);
}
}
主意:不能在页面的InitializeComponent()里对DataGrid声明,因为实际的情况是,我要生成若干个不同列数的DataGrid加到panel里面去。
因为各DataGrid的列数不尽相同,所以,对DataGrid的定义和数据绑定还要在一起。
Panel Panel1;
Panel1=(Panel)e.Item.Page.FindControl("Panel1");
Panel1.Controls.Clear();
CreateGrid(Panel1);
就是重新生成DataGrid并且重新绑定啊。各位如果有时间,可以亲自试一下这个例子。
private void CreateGrid(Panel Panel1 )
{
DataGrid dg=new DataGrid();
ButtonColumn col=new ButtonColumn();
col.DataTextField="ISBN";
dg.ItemCommand+=new DataGridCommandEventHandler(dg_ItemCommand);
dg.Columns.Add(col);
dg.DataSource =this.GetDataSource();
dg.DataBind();
Panel1.Controls.Add(dg);
}一次...你都建个新的...你...的资源不会耗尽啊
解决的办法是你把DataGrid dg=new DataGrid();放在外面,应该可以解决这个问题!不过你的程序应该还会出现别的问题,我总觉得怪怪的,但是又说不明白,你自己试试看。
public class WebForm30 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
DataGrid dg=new DataGrid();
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateGrid(Panel1);
}
http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx
{
if (IsNotPostBack)
{
this.CreateGrid(Panel1);
}
}