Command对象删除数据 不懂

waite1314 2012-10-13 11:07:19
public SqlConnection Getconnection()
{
string str = ConfigurationManager.AppSettings["ee"].ToString();
SqlConnection myCon = new SqlConnection(str);
return myCon;
}
public void bind()
{
SqlConnection myCon = Getconnection();
myCon.Open();
string str = "select * from tb_Class";
SqlCommand myCom = new SqlCommand(str, myCon);
SqlDataAdapter myDa = new SqlDataAdapter(myCom);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "ClassID" };
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myCon.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection myCon = Getconnection();
myCon.Open();
int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string str = "delete from tb_Class where ClassID=" + ClassID;
SqlCommand myCom = new SqlCommand(str, myCon);
myCom.ExecuteNonQuery();
myCom.Dispose();
myCon.Close();
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
这段代码是,弹出一个"确定要删除吗?"的小窗口!!
具体的像(e.Row.RowType == DataControlRowType.DataRow)
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add
是什么意思

}
...全文
93 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
waite1314 2012-10-14
  • 打赏
  • 举报
回复
明白了
暖枫无敌 2012-10-13
  • 打赏
  • 举报
回复
在GridView的行绑定事件中,判断如果行是数据行的话,通过FindControl找到LinkButton,然后为其添加客户端click事件,事件执行的代码就是弹出一个提示框,仅此而已。
bdmh 2012-10-13
  • 打赏
  • 举报
回复
动态添加onclick事件
也就是点击时执行脚本return confirm('确定要删除吗?'
licai1210 2012-10-13
  • 打赏
  • 举报
回复
代码的意思和你看到的效果一样,就是给一个a标签添加一个客户端单击事件
chinagzu 2012-10-13
  • 打赏
  • 举报
回复
e.Row.RowType == DataControlRowType.DataRow

这个判断语句的意思就是判断当前行是不是数据绑定行

((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add

就像前幾樓說的給linkbutton按鈕添加一個js事件。彈出确定要删除吗這個窗口。

61,655

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧