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?
Membership.GetUser(UserId)
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:
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:
Even simpler is the shortcut SelectedValue of the DetailsView: