protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
LinkButton lbtn1 = (LinkButton)GridView1.FindControl("LinkButton1");
if (lbtn1.CommandName == "Delete")//这就是红字行,
{
int index = Convert.ToInt32(lbtn1.CommandArgument);
string sqlstr = "delete from xianka where id='" + GridView1.Rows[index].ToString() + "'";
OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pcdb"].ConnectionString);
OleDbCommand cmd = new OleDbCommand(sqlstr, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
}
LinkButton1是我在模板列中我加的一个linkbutton1,text=删除,commandname设置成Delete,我的目的是点linkbutton1后提示确认删除对话框,前台js实现,这个没问题,就是点击确定后,出现的编译错误!
{
LinkButton lbtn1 = (LinkButton)GridView1.FindControl("LinkButton1");
if (lbtn1.CommandName == "Delete")//这就是红字行,
{
int index = Convert.ToInt32(lbtn1.CommandArgument);
string sqlstr = "delete from xianka where id='" + GridView1.Rows[index].ToString() + "'";
OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pcdb"].ConnectionString);
OleDbCommand cmd = new OleDbCommand(sqlstr, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
}
LinkButton1是我在模板列中我加的一个linkbutton1,text=删除,commandname设置成Delete,我的目的是点linkbutton1后提示确认删除对话框,前台js实现,这个没问题,就是点击确定后,出现的编译错误!
解决方案 »
- FileUpLoad上传图片保存到服务器后,如何即时显示图片
- 请教高手一个关于datalist中嵌套按钮的问题 急~~
- 再发一遍,看看有没人回答!!!
- 如何得到用户控件中的某个控件的控件值?
- 邮件系统的上传附件是怎么实现的?
- 怎么在gridview中取得指定的某一行某一列的值呢?
- 如何把本地指定目录的指定文件上传到服务器上,最好在服务器上的指定目录下,我尝试过用一些httpXXX类的SaveAs方法
- 本人初学asp.net,问个简单的送分题
- 关于ASP.NET 的问题,我看书越看越糊涂了????
- 正则表达式验证时间的疑惑?请大家进来看看!
- 用过JAVASCRIPT后样式没有了
- 绑定数据的下拉可多选怎么实现?
int index = Convert.ToInt32(lbtn1.CommandArgument); 这是红字行
这句又出错了,错误提示
异常详细信息: System.FormatException: 输入字符串的格式不正确。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
}
}
是你这个LinkButton lbtn1 = (LinkButton)GridView1.FindControl("LinkButton1");
lbtn1找不到,你这样是找不到的
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
LinkButton lbtn1 = (LinkButton)e.Row.FindControl("LinkButton1");
lbtn1.CommandArgument=drv["id"];
}
}
{
if (e.CommandName == "Delete")
{
int id = Convert.ToInt32(e.CommandArgument);
//删除对应id的数据
}
if (e.CommandName == "View")
{
int id = Convert.ToInt32(e.CommandArgument);
}
...
}
//或者
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
switch(e.CommandName)
{
case "Delete":
//删除对应id的数据
break;
}
}
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
string sqlstr = "delete from xianka where id='" + GridView1.DataKeys[index].Value.ToString() + "'";
OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pcdb"].ConnectionString);
OleDbCommand cmd = new OleDbCommand(sqlstr, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind(); }感觉应该正确,但是倒数第3行报错,是不是SQL语句不对吗?但是应该没错误吧!