ASP.NET:使用 Gridview 删除用户配置文件

发布于 2024-12-16 21:02:22 字数 793 浏览 0 评论 0原文

我有一个这样的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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-12-23 21:02:22

在 GridView 上设置 DataKeyNames,如下所示:DataKeyNames="ProviderUserKey" 并将 OnDeleteCommand 设置为如下所示的某种方法:OnDeleteCommand="gvUser_DeleteCommand"

然后在后面的代码中实现 onDelete 方法,如下所示:

protected void rgUser_DeleteCommand(object source, GridCommandEventArgs e)
{
    GridDataItem gdiItem = (GridDataItem)e.Item;
    try
    {
        string strUserKey = gdiItem.GetDataKeyValue("ProviderUserKey").ToString();
        Guid guiUserKey = new Guid(strUserKey);
        string strUserName = Membership.GetUser(guiUserKey).UserName.ToString();

        Membership.DeleteUser(strUserName);
    }
    catch (Exception ex)
    {
        //handle exception
    }
}

只是为了了解更多信息,您绑定到 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:

protected void rgUser_DeleteCommand(object source, GridCommandEventArgs e)
{
    GridDataItem gdiItem = (GridDataItem)e.Item;
    try
    {
        string strUserKey = gdiItem.GetDataKeyValue("ProviderUserKey").ToString();
        Guid guiUserKey = new Guid(strUserKey);
        string strUserName = Membership.GetUser(guiUserKey).UserName.ToString();

        Membership.DeleteUser(strUserName);
    }
    catch (Exception ex)
    {
        //handle exception
    }
}

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 the OnDeleting event because it is fired before the record is deleted. In the OnDeleting event you can add your custom delete code and then cancel the event, but you can't do theat in the OnDeleted event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文