我想隐藏位于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;
}
}
答案 0 :(得分:0)
尝试一下:
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
或从模板中删除按钮本身。