[英]ASP.NET C# Add/Update User to Role
Hi all I have a page that shows Accommodation info and then the UserID of the person that created that information in a DetailsView. 大家好我有一个页面显示住宿信息,然后是在DetailsView中创建该信息的人的UserID。
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. 我还有一个按钮,应该看看UserID和单击时将该userID转换为用户名,以便我可以使用该用户名将人员角色更改为租用者。 However I am unsure using C# how I can grab the UserID from details view do the conversion and update the role. 但是我不确定使用C#如何从详细信息视图中获取UserID进行转换并更新角色。 Any ideas? 有任何想法吗?
Mark 标记
@Tim @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. 我在详细信息视图下方的页面上添加了一个按钮,并将commandName设置为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. 我是ASP和C#的新手,但需要此功能才能完成大学任务。 Any Idea? 任何想法?
Membership.GetUser(UserId) Membership.GetUser(用户ID)
MembershipUser memUser = Membership.GetUser(UserId);
Roles.AddUserToRole(memUser.UserName, "Renter");
Roles.AddUserToRole 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 下面是一个如何获取DetailsView的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. 您应该将Button的CommandName设置为其功能,并在代码隐藏中处理DetailsView.ItemCommand事件。 There you can get your UserID in the following way: 在那里,您可以通过以下方式获取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");
}
}
As Cem has mentioned you should set the DetailsView's DataKey Property . 正如Cem所提到的,你应该设置DetailsView的DataKey属性 。 Then you can also get the PK of the current record in the following way: 然后您还可以通过以下方式获取当前记录的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();
Even simpler is the shortcut SelectedValue of the DetailsView: 更简单的是DetailsView的快捷方式SelectedValue :
String UserID= DetailsView1.SelectedValue.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.