以下代码,是通过遍历GridViewRow中的checked来确定选中的数据行,是否要被删除。
·但是我却不知道该怎么获得行的ID了?请高手指点!
------
protected void Button3_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
if (chk.Checked)
{
string id =???
}
}
}
·但是我却不知道该怎么获得行的ID了?请高手指点!
------
protected void Button3_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
if (chk.Checked)
{
string id =???
}
}
}
解决方案 »
- 如何跳转页面返回 保存数据
- 利用ajax的问题
- asp.net 实现从数据库下载文件
- IE6实现水平翻转图片!IE6 6 6 6 6 6
- 如何在winform windows窗体中使用asp.net/HTML?
- 关于GirdView隐藏行的问题。
- ASP。NET连接DB2 非常急的问题!!!!!!各位兄弟姐妹救急啊
- 如何在iframe里写代码使父页执行buttom_onclick事件。(就剩下50分了,帮帮我,要不我会死的!~)
- 如何写AspnetMenu1.1中的菜单项单击事件......
- 我的IIS5.1设置完了,输入http://192.168.0.202(本机ip)可以显主示页;但输入http://localhost则不能显示主页为何?
- 一个简单但很郁闷的问题(菜鸟问题)
- |M| 第八贴 学习URL改写和相关知识 如何给IIS 添加 映身射 让他可以执行.aspm .107 dir 这样了的网页
{
CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
if (chk.Checked)
{
string id =GridView1.DataKeys[i].Values[0].ToString();
}
}
这样就差不多了
所以用e.RowIndex是不行的!
{
CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
if (chk.Checked)
{
string id =GridView1.DataKeys[i].Values[0].ToString();
}
}对了
---基于foreach的---
foreach (GridViewRow GR in GridView1.Rows)
{
CheckBox chk0 = (CheckBox)GR.Cells[6].FindControl("itemchk");
if (chk0.Checked)
{
string id = GridView1.DataKeys[GR.RowIndex].Values[0].ToString();
string sqlstr = "delete from authors where au_id='" + id + "'"; //以下数据操作可以封装到全局类
string constr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(constr);
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand(sqlstr, sqlCon);
sqlCom.ExecuteNonQuery();
}
}
---基于FOR的---
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[6].FindControl("itemchk");
if (chk.Checked)
{
string id = GridView1.DataKeys[i].Values[0].ToString();
string sqlstr = "delete from authors where au_id='" + id + "'"; string constr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(constr);
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand(sqlstr, sqlCon);
sqlCom.ExecuteNonQuery();
}
}