Asp.net MembershipUser 从 GridView 检索数据
我对这段代码感到困惑。
我需要设置 IsApproved = true;当我在 GridView 中选择复选框时,对于用户(使用 MembershipUser)。
事件处理程序 uxRoleCheckBoxSelector_CheckChanged 它设置在实际的 CheckBox 上。
你们能告诉我我做错了什么吗?
脚本不会生成任何异常,但不起作用。感谢您的支持!
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the Lable for an User name in a row
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
GridViewRow user = (GridViewRow)myUserName.NamingContainer;
MembershipUser myUser = (MembershipUser)user.DataItem;
if (activeCheckBox.Checked == true)
{
uxMessageDisplayer.Text = "T - Aproved User";
myUser.IsApproved = true;
Membership.UpdateUser(myUser);
}
else
{
uxMessageDisplayer.Text = "F - NOT Aproved User";
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
}
}
经过调整和您的建议后,脚本进行了编辑。现在正在工作...希望可以帮助别人:-)
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the name for the User from a label
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
// Keep User's name in a variable
string UserName = myUserName.Text;
// Create an Object of type MembershipUser and associate its User's name
MembershipUser myUser = Membership.GetUser(UserName);
// Check if a CheckBox is selected or not for a User
if (activeCheckBox.Checked == true)
{
// Set status for an User
myUser.IsApproved = true;
// Save status
Membership.UpdateUser(myUser);
// Display message
uxMessageDisplayer.Text = string.Format("The User {0} has been activated.", UserName);
}
else
{
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
uxMessageDisplayer.Text = string.Format("The User {0} has been deactivated. User cannot use this System.", UserName);
}
}
I am puzzling with this code.
I need to set IsApproved = true; for a User (using MembershipUser) when I select a CheckBox in a GridView.
The event handler uxRoleCheckBoxSelector_CheckChanged it is set on the actual CheckBox.
Could you tell me guys what I am doing wrong?
Script does not generate any Exception but does not work. Thanks for your support!
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the Lable for an User name in a row
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
GridViewRow user = (GridViewRow)myUserName.NamingContainer;
MembershipUser myUser = (MembershipUser)user.DataItem;
if (activeCheckBox.Checked == true)
{
uxMessageDisplayer.Text = "T - Aproved User";
myUser.IsApproved = true;
Membership.UpdateUser(myUser);
}
else
{
uxMessageDisplayer.Text = "F - NOT Aproved User";
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
}
}
After come tweaks and your advices here the script edited. Now is working... Hope can help someone else :-)
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the name for the User from a label
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
// Keep User's name in a variable
string UserName = myUserName.Text;
// Create an Object of type MembershipUser and associate its User's name
MembershipUser myUser = Membership.GetUser(UserName);
// Check if a CheckBox is selected or not for a User
if (activeCheckBox.Checked == true)
{
// Set status for an User
myUser.IsApproved = true;
// Save status
Membership.UpdateUser(myUser);
// Display message
uxMessageDisplayer.Text = string.Format("The User {0} has been activated.", UserName);
}
else
{
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
uxMessageDisplayer.Text = string.Format("The User {0} has been deactivated. User cannot use this System.", UserName);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定该事件确实被触发(因此该方法实际上正在运行)吗?
您是否在复选框上设置了 AutoPostBack = true ?如果没有,并且您正在使用按钮进行回发,我不确定该事件是否会因复选框而被触发......但如果我错了,请纠正我。
Are you sure this event is actually getting fired (and thus this method is actually running)?
Do you have a AutoPostBack = true set on the checkbox? If not, and you are using a button to do a postback, I am not sure if that event will get fired for the checkbox... but correct me if I'm wrong.