ASP.NET C#将用户添加/更新到角色
大家好,我有一个页面,显示住宿信息,然后是在DetailsView中创建该信息的人的用户ID 我还有一个按钮,可以查看该用户ID,单击时将该用户ID转换为用户名,这样我就可以使用该用户名将persons角色更改为renter。但是,我不确定如何使用C#从details视图中获取用户ID并进行转换和更新角色。有什么想法吗 标记 @提姆 以下是我添加的代码:ASP.NET C#将用户添加/更新到角色,c#,asp.net,roles,C#,Asp.net,Roles,大家好,我有一个页面,显示住宿信息,然后是在DetailsView中创建该信息的人的用户ID 我还有一个按钮,可以查看该用户ID,单击时将该用户ID转换为用户名,这样我就可以使用该用户名将persons角色更改为renter。但是,我不确定如何使用C#从details视图中获取用户ID并进行转换和更新角色。有什么想法吗 标记 @提姆 以下是我添加的代码: public partial class adminonly_approval : System.Web.UI.Page { prot
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");
}
}
我在details视图下面的页面上添加了一个按钮,并将commandName设置为setToEnter。当我点击按钮时,虽然它没有改变角色。我是ASP和C的新手,但是大学作业需要这个功能。
有什么想法吗
下面是一个关于如何获取DetailsView的边界字段值的示例:
您应该将按钮的CommandName设置为其函数,并在codebehind中处理DetailsView.ItemCommand事件。您可以通过以下方式获取您的用户ID:
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");
}
}
// 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();
正如Cem所提到的,您应该设置DetailsView。您还可以通过以下方式获取当前记录的主键:
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");
}
}
// 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();
更简单的是DetailsView的快捷方式:
String UserID= DetailsView1.SelectedValue.ToString();
嗨,蒂姆,谢谢你的回复,我看到它得到的是当前的UsersID,而不是详细信息视图UserID字段中的UsersID。你知道我是怎么做到的吗?@Mark:它没有得到当前用户的UserID。它只是显示了当您拥有用户ID时将用户添加到角色的方法。您应该向我们展示一些代码,或者告诉我们您在获取用户ID时遇到的问题。DataKey也可以工作。我的意思是,您不必在DetailsView中查找用户ID的单元格@蒂姆:我已经将代码添加到我原来添加的帖子中,现在遇到了一些问题。我看到了你在cem提到后添加的其他代码,但你说它得到了PK,但这是Accom_ID,在本例中不是UserID@Mark当前位置您可以随时使用我的第一种方法,您已将其复制到您的问题中。但是你没有提到你有什么问题。我怀疑它实际上是你详细资料视图第十行的第十个单元格。我想这是第十排,但第二个单元格<代码>字符串UserID=row.Cells[1]。文本代码>