1.动态定义按钮
protected void Page_Load(object sender, EventArgs e)
{ SqlConnection con = dbcon.creatConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from userid", con);
DataSet ds = new DataSet();
sda.Fill(ds, "userid");
con.Close();
//存放表中的主建到DataGrid1中的索引中
this.DataGrid1.DataKeyField = "id";
this.DataGrid1.DataSource = ds.Tables["userid"];
this.DataGrid1.DataBind(); ButtonColumn BoundColTotalN = new ButtonColumn();
//内容
BoundColTotalN.Text = "删除";
//命令
BoundColTotalN.CommandName = "Delete";
//按钮类型
BoundColTotalN.ButtonType = ButtonColumnType.PushButton; ;
//把按钮列添加到第1列后
this.DataGrid1.Columns.AddAt(0, BoundColTotalN);
this.DataGrid1.DataBind();
}删除事件:
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string empid = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con = dbcon.creatConnection();
con.Open();
SqlCommand cmd = new SqlCommand("delete from userid where id = "+empid ,con);
cmd.ExecuteNonQuery();
}问题:
1.以上代码可以运行,但是把Page_Load的代码放在其它地方就不能运行,例如:Button12_Click。为什么。谢谢大家。
protected void Page_Load(object sender, EventArgs e)
{ SqlConnection con = dbcon.creatConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from userid", con);
DataSet ds = new DataSet();
sda.Fill(ds, "userid");
con.Close();
//存放表中的主建到DataGrid1中的索引中
this.DataGrid1.DataKeyField = "id";
this.DataGrid1.DataSource = ds.Tables["userid"];
this.DataGrid1.DataBind(); ButtonColumn BoundColTotalN = new ButtonColumn();
//内容
BoundColTotalN.Text = "删除";
//命令
BoundColTotalN.CommandName = "Delete";
//按钮类型
BoundColTotalN.ButtonType = ButtonColumnType.PushButton; ;
//把按钮列添加到第1列后
this.DataGrid1.Columns.AddAt(0, BoundColTotalN);
this.DataGrid1.DataBind();
}删除事件:
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string empid = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con = dbcon.creatConnection();
con.Open();
SqlCommand cmd = new SqlCommand("delete from userid where id = "+empid ,con);
cmd.ExecuteNonQuery();
}问题:
1.以上代码可以运行,但是把Page_Load的代码放在其它地方就不能运行,例如:Button12_Click。为什么。谢谢大家。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货