ASP.NET:使用 Gridview 删除用户配置文件
我有一个这样的SQL;
select B.LoweredUserName
from USER_BAYI A, aspnet_Users B, aspnet_Membership C
where B.UserId = C.UserId
AND B.UserName = A.USERNAME
它正在获取我的会员数据库中的所有用户名。
我想使用 Gridview 的删除属性从我的会员数据库中删除该用户。
我在 sqldatasource 中的删除命令是;
DeleteCommand="DELETE FROM aspnet_Users
WHERE LoweredUserName = @LoweredUserName" ondeleted="SqlDataSource1_Deleted"
protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
DbCommand cmd = e.Command;
string username = cmd.Parameters["@LoweredUserName"].Value.ToString();
Membership.DeleteUser(username);
}
但这甚至无法编译。我哪里做错了?或者你能告诉我一种方法吗?
I have a SQL like this;
select B.LoweredUserName
from USER_BAYI A, aspnet_Users B, aspnet_Membership C
where B.UserId = C.UserId
AND B.UserName = A.USERNAME
It's getting all username in my membership database.
I want to user Gridview's delete properties for delete this users from my membership database.
My delete commands in sqldatasource is;
DeleteCommand="DELETE FROM aspnet_Users
WHERE LoweredUserName = @LoweredUserName" ondeleted="SqlDataSource1_Deleted"
protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
DbCommand cmd = e.Command;
string username = cmd.Parameters["@LoweredUserName"].Value.ToString();
Membership.DeleteUser(username);
}
But this doesn't even compile. Where am I doing wrong? Or can you show me a way for doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GridView 上设置 DataKeyNames,如下所示:
DataKeyNames="ProviderUserKey"
并将 OnDeleteCommand 设置为如下所示的某种方法:OnDeleteCommand="gvUser_DeleteCommand"
。然后在后面的代码中实现 onDelete 方法,如下所示:
只是为了了解更多信息,您绑定到 SqlDataSource 上的错误事件。
OnDeleted
事件在删除记录后触发,您需要OnDeleting
事件,因为它在删除记录之前触发。在OnDeleting
事件中,您可以添加自定义删除代码,然后取消该事件,但您不能在OnDeleted
事件中执行此操作。Set the DataKeyNames on the GridView like this:
DataKeyNames="ProviderUserKey"
and set the OnDeleteCommand to some method likie this:OnDeleteCommand="gvUser_DeleteCommand"
.Then in the code behind implement the onDelete method like this:
Just for more info, you are binding to the wrong event on the SqlDataSource. The
OnDeleted
event is fired after the record is deleted, and you need theOnDeleting
event because it is fired before the record is deleted. In theOnDeleting
event you can add your custom delete code and then cancel the event, but you can't do theat in theOnDeleted
event.