ASP.NET C # Add / Update User to Role

Hello everyone. I have a page that shows information about accommodation, and then the user ID of the person who created this information in DetailsView.

I also have a button that should look at what UserID is and when clicked so that this userID will convert it to a username so that I can then use this username to change the role of individuals to the tenant. However, I'm not sure, using C #, how can I grab the UserID from the details view, do the conversion, and update the role. Any ideas?

Mark

@Tim

Here is the code I 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 added a button on the page below the details view and set the name of the SetToRenter command. When I press the button, although it does not change the role. I'm new to ASP and C #, but need this feature for a university assignment. Any ideas?

+4
source share
1 answer

Membership.GetUser (UserId)

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

Roles.AddUserToRole

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

You must set the Button CommandName of your function and handle the DetailsView.ItemCommand event in your code. There you can get your UserID as follows:

 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 mentioned, you must set the DetailsView DataKey Property . Then you can also get the PK of the current record as follows:

 // 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 SelectedValue DetailsView shortcut:

 String UserID= DetailsView1.SelectedValue.ToString(); 
+12
source

Source: https://habr.com/ru/post/1346623/


All Articles