ASP.NET C# 添加/更新用户到角色

发布于 2024-10-29 18:15:44 字数 996 浏览 6 评论 0原文

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

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

马克

@Tim

这是我添加的代码:

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# 新手,但需要此功能来完成大学作业。 有什么想法吗?

Hi all I have a page that shows Accommodation info and then the UserID of the person that created that information in a DetailsView.

I also have a button that should look at that that UserID and when clicked take that userID convert it to a username so that then I can use that username to change the persons role to a renter. However I am unsure using C# how I can grab the UserID from details view do the conversion and update the role. Any ideas?

Mark

@Tim

Here is the code I have added:

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");
    }
}

I Have added a button on the page below the details view and set the commandName to SetToRenter. When I click the button though it isn't changing the roles. Im new to ASP and C# but need this feature for a university assignment.
Any Idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧竹 2024-11-05 18:15:44

Membership.GetUser(UserId)

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

Roles.AddUserToRole

以下是有关如何获取您的值的示例详细信息视图的绑定字段:
http://msdn.microsoft。 com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx

您应该将按钮的 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 属性。然后,您还可以通过以下方式获取当前记录的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();

更简单的是快捷方式SelectedValue 的详细信息视图:

String UserID= DetailsView1.SelectedValue.ToString();

Membership.GetUser(UserId)

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

Roles.AddUserToRole

Here is an example on how to get values of your DetailsView's BoundFields:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx

You should set the Button's CommandName to its function and handle the DetailsView.ItemCommand Event in codebehind. There you can get your UserID in the following way:

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");
    }
}

As Cem has mentioned you should set the DetailsView's DataKey Property. Then you can also get the PK of the current record in the following way:

// 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();

Even simpler is the shortcut SelectedValue of the DetailsView:

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