Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET C# Add/Update User to Role

Tags:

c#

asp.net

roles

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?

like image 932
Mark Avatar asked Dec 16 '22 15:12

Mark


1 Answers

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();
like image 62
Tim Schmelter Avatar answered Dec 21 '22 22:12

Tim Schmelter