[英]How to hide the “Edit” button in an asp.net FormView control?
我想隐藏位于FormView控件的ItemTemplate中的“ EditButton”。
这是我尝试过的FormView的OnDataBound代码:
protected void fvPhaudDets_OnDataBound(object sender, EventArgs e)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
PrincipalSearchResult<Principal> groups = UserPrincipal.FindByIdentity(ctx, User.Identity.Name).GetAuthorizationGroups();
IEnumerable<string> groupNames = groups.Select(x => x.Name);
string mode = fvPhaudDets.CurrentMode.ToString();
lblCrntMode.Text = mode;
if (fvPhaudDets.CurrentMode == FormViewMode.ReadOnly)
{
Button editbtn = fvPhaudDets.FindControl("EditButton") as Button;
//Determine authorization based on the user's AD security groups
if (groupNames.Contains("SecGroup1"))
{
editbtn.Visible = false;
}
else
{
editbtn.Visible = true;
}
}
}
这是我得到的错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
也许问题在于代码是在页面上呈现整个FormView之前执行的?
如果当前用户是SecGroup1的成员,如何修改代码以确保FormView的ItemTemplate中的“ EditButton”被隐藏?
- 编辑 -
这按预期工作...
if (fvPhaudDets.CurrentMode == FormViewMode.ReadOnly)
{
LinkButton editbtn = fvPhaudDets.FindControl("EditButton") as LinkButton;
if (editbtn != null && groupNames.Contains("SecGroup1"))
{
editbtn.Visible = true;
}
}
尝试这个:
protected void fvPhaudDets_DataBound(object sender, EventArgs e)
{
((LinkButton) ((FormView)sender).FindControl("EditButton")).Visible = false;// Hides Edit button
((LinkButton) ((FormView)sender).FindControl("NewButton")).Visible = false;// Hides New button
((LinkButton) ((FormView)sender).FindControl("DeleteButton")).Visible = false;// Hides Delete button
}
另一个解决方案:编辑模板,然后将链接按钮的属性更改为Visible= false
或从模板中删除按钮本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.