ASP.NET C#添加/更新用户到角色 [英] ASP.NET C# Add/Update User to Role
问题描述
大家好我有一个页面,显示住宿信息,然后创建在一个DetailsView该信息的人的用户名。
Hi all I have a page that shows Accommodation info and then the UserID of the person that created that information in a DetailsView.
我也有一个按钮,应该看的那个用户名和点击时采取用户标识将其转换为一个用户名,这样然后我可以使用该用户名来改变人的角色,以房客的。但是我不能确定使用C#我怎么能抓住用户ID从细节来看做转换和更新的作用。任何想法?
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?
标记
@Tim
下面是code我已经加入:
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");
}
}
下面的详细信息查看和设置的CommandName为SetToRenter我已经添加了网页上的按钮。当我点击按钮虽然未改变的角色。进出口新的ASP和C#,但需要此功能的大学分配。
任何想法?
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?
推荐答案
MembershipUser memUser = Membership.GetUser(UserId);
Roles.AddUserToRole(memUser.UserName, "Renter");
下面是关于如何让你的DetailsView的绑定列的值的示例:
<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx
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
您应该设置按钮的CommandName的其功能和codebehind处理DetailsView.ItemCommand事件。在那里,你可以得到你的用户名以下列方式:
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");
}
}
由于杰姆提到你应该设置DetailsView的的 DataKey属性的。然后,你还可以得到当前记录的PK以下列方式:
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();
更简单的是快捷键<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.selectedvalue.aspx\"相对=nofollow> DetailsView控件的SelectedValue的:
String UserID= DetailsView1.SelectedValue.ToString();
这篇关于ASP.NET C#添加/更新用户到角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!