【问题标题】:ASP.NET C# Add/Update User to RoleASP.NET C# 添加/更新用户到角色
【发布时间】:2011-07-29 11:52:08
【问题描述】:

大家好,我有一个页面显示住宿信息,然后是在 DetailsView 中创建该信息的人的用户 ID。

我还有一个按钮,应该查看那个用户 ID,当点击该用户 ID 时,将该用户 ID 转换为用户名,以便我可以使用该用户名将人员角色更改为租户。但是我不确定使用 C# 如何从详细信息视图中获取用户 ID 进行转换并更新角色。有什么想法吗?

标记

@蒂姆

这是我添加的代码:

public partial class adminonly_approval : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void DetailsView1_ItemCommand(Object sender, DetailsViewCommandEventArgs e){
    if (e.CommandName == "SetToRenter")
    {
        // if UserID is in second row:
        DetailsViewRow row = DetailsView1.Rows[9];

        // Get the Username from the appropriate cell.
        // In this example, the Username is in the second cell  
        String UserID = row.Cells[9].Text;
        MembershipUser memUser = Membership.GetUser(UserID);
        Roles.AddUserToRole(memUser.UserName, "renter");
    }
}

我在详细信息视图下方的页面上添加了一个按钮,并将 commandName 设置为 SetToRenter。当我单击按钮时,虽然它没有改变角色。我是 ASP 和 C# 的新手,但在大学作业中需要此功能。 任何想法?

【问题讨论】:

    标签: c# asp.net roles


    【解决方案1】:

    Membership.GetUser(UserId)

    MembershipUser memUser = Membership.GetUser(UserId);
    Roles.AddUserToRole(memUser.UserName, "Renter");
    

    Roles.AddUserToRole

    这是一个关于如何获取 DetailsView 的 BoundFields 值的示例: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx

    您应该将 Button 的 CommandName 设置为其函数并在代码隐藏中处理 DetailsView.ItemCommand 事件。您可以通过以下方式获取您的 UserID:

    void DetailsView1_ItemCommand(Object sender, DetailsViewCommandEventArgs e){
        if (e.CommandName == "SetToRenter")
        {
            // if UserID is in second row:
            DetailsViewRow row = DetailsView1.Rows[1];
    
            // Get the Username from the appropriate cell.
            // In this example, the Username is in the second cell  
            String UserID = row.Cells[1].Text;
            MembershipUser memUser = Membership.GetUser(UserId);
            Roles.AddUserToRole(memUser.UserName, "Renter");
        }
    }
    

    正如 Cem 所提到的,您应该设置 DetailsView 的 DataKey Property。那么也可以通过以下方式获取当前记录的PK:

    // Get the ArrayList objects that represent the key fields
    ArrayList keys = (ArrayList)(DetailsView1.DataKey.Values).Keys;
    // Get the key field for the current record. 
    String UserID = keys[0].ToString();
    

    更简单的是DetailsView的快捷方式SelectedValue

    String UserID= DetailsView1.SelectedValue.ToString();
    

    【讨论】:

    • 嗨蒂姆,感谢您的回复,我看到获取当前用户 ID 而不是详细信息视图用户 ID 字段中的一个,知道我是如何做到的吗?
    • @Mark: 没有得到当前用户的UserID。它只是显示了当您拥有 UserID 时将用户添加到角色的方法。您应该向我们展示一些代码或告诉我们您在获取 UserID 时遇到的问题。
    • DataKey 也可以工作。我的意思是您不必在 DetailsView 中找到UserID 的单元格。 msdn.microsoft.com/en-us/library/…
    • @Tim 我已将代码添加到我添加的原始帖子中,但遇到了一些问题。我看到你在 cem 提到它之后添加的其他代码,但你说得到了 PK,但在这种情况下是 Accom_ID 而不是 UserID
    • @Mark:你总是可以使用我的第一种方法,你已经复制到你的问题中。但是你没有提到你有什么样的问题。我怀疑它实际上是您的详细信息视图第 10 行中的第 10 个单元格。我会假设它是第 10 行,但是是第二个单元格。 String UserID = row.Cells[1].Text;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多