ASP.NET C# 添加/更新用户到角色
大家好,我有一个页面显示住宿信息,然后显示在详细信息视图中创建该信息的人的用户 ID。
我还有一个按钮,应该查看该用户 ID,单击该按钮时将该用户 ID 转换为用户名,以便我可以使用该用户名将人员角色更改为租户。但是我不确定使用 C# 如何从详细信息视图中获取 UserID 进行转换并更新角色。有什么想法吗?
马克
@Tim
这是我添加的代码:
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# 新手,但需要此功能来完成大学作业。 有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Membership.GetUser(UserId)
Roles.AddUserToRole
以下是有关如何获取您的值的示例详细信息视图的绑定字段:
http://msdn.microsoft。 com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx
您应该将按钮的 CommandName 设置为其函数,并在代码隐藏中处理 DetailsView.ItemCommand 事件。您可以通过以下方式获取您的 UserID:
正如 Cem 所提到的,您应该设置 DetailsView 的 DataKey 属性。然后,您还可以通过以下方式获取当前记录的PK:
更简单的是快捷方式SelectedValue 的详细信息视图:
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: