ASP.NET中RowDeleting删除行的后台代码?

2025-01-18 16:02:59
推荐回答(3个)
回答1:



无标题页





































友情链接管理

网站名称:


ControlToValidate="TextBox1" ErrorMessage="网址名称不能为空">

网站地址:

Width="300px">
ControlToValidate="TextBox2" ErrorMessage="网址格式不正确"
ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?">



    


Width="726px" CellPadding="4" ForeColor="#333333" GridLines="None"
DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">






CausesValidation="False" />


CommandArgument='<%# Eval("id") %>' CausesValidation="False">删除
















namespace xzbags.Admin
{
public partial class FrientsManaber : System.Web.UI.Page
{
User uu = new User();
UsersModel um = new UsersModel();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定
public void bind()
{
DataTable dt = uu.Friends();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.ToString();
string address = TextBox2.Text.ToString();
DateTime time = Convert.ToDateTime(System.DateTime.Now.ToString("yy-MM-dd hh:mm:ss"));

um.WName = name;
um.Waddress = address;
um.time = time;

if (uu.AddFriends(um) > 0)
{
bind();
TextBox1.Text = "";
TextBox2.Text = "";
}

}

protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument.ToString());
if (e.CommandName == "shan")
{
um.id = id;
if (uu.deletefriends(um, id) > 0)
{
bind();
}
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string time = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
um.id = id;
um.WName = name;
um.Waddress =address;
if (uu.updatefriends(um, id) > 0)
{
GridView1.EditIndex = -1;
bind();
}
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
}
}
调用的类里的数据库语句。。你创建个类,自己写增删改语句吧。。
另外,团IDC网上有许多产品团购,便宜有口碑

回答2:

语句看上去没什么错,如果你的图书编号是整型的话,建议把单引号去掉!除此之外,请确保你的连接能够连接上数据,最好把错误信息放上来!

---看到你的图片了
索引超过,你设置了gridView的DataKeys没有

回答3:

调试运行一下嘛。
看一看 GridView1.DataKeys[e.RowIndex].Value
到底值是什么。
如果报错,就看看 GridView1.DataKeys[e.RowIndex-1].Value