简体   繁体   中英

ASP.NET C# Add User to a Role

Hi All I a Details View containing UserID and other attributes. I want on button click to be able to update the UserID shown with the Role Renter. I therefore set a button with the CommandName SetToRenter and have the following code in the C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

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")
    {
        DetailsViewRow row = DetailsView1.Rows[0];
        String UserID = row.Cells[0].Text;
        MembershipUser memUser = Membership.GetUser(UserID);
        Roles.AddUserToRole(memUser.UserName, "renter");
    }
}

}

On click of the button it just refreshes the code and is failing to set the role to renter.

Any Ideas?

您在调试代码时是否进入ItemCommand方法,如果这样做,或者如果您检查数据库,是否已经将“ renter”角色添加到了memUser的asp.net角色表中,但未显示该更改。由于某些晦涩的原因,DetailsView。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM