0

I would like to hide the "EditButton" located in the ItemTemplate of a FormView control.

Here's is the OnDataBound code of the FormView that I have tried:

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;
            }
        }
    }

Here is the error I get:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Maybe the issue is that the code is being executed prior to the entire FormView being rendered on the page?

How can I modify the code to make sure the "EditButton" in the FormView's ItemTemplate is hidden if the current user is a member of SecGroup1?

--EDIT--

This works as expected...

if (fvPhaudDets.CurrentMode == FormViewMode.ReadOnly)
    {
        LinkButton editbtn = fvPhaudDets.FindControl("EditButton") as LinkButton;

        if (editbtn != null && groupNames.Contains("SecGroup1"))
        {
            editbtn.Visible = true;
        }
    }
user1916528
  • 287
  • 3
  • 19
  • Which line throws your error? – gunr2171 Nov 06 '18 at 21:24
  • It happens on "editbtn.visible = false;" in the if statement. – user1916528 Nov 06 '18 at 22:36
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dee-see Nov 06 '18 at 22:38
  • Thanks for the link @dee-dee. I understand now that the evaluation starts before the FormView is completely loaded, but doesn't complete until it is. So, a simple modification to evaluate whether or not the button was null is all it took. I'll post the change for others who may find this. – user1916528 Nov 06 '18 at 23:18
  • can you post the formview asp.net UI code also? – Gauravsa Nov 06 '18 at 23:54

1 Answers1

0

Try this:

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
        }

Another solution: edit the template then either change the property of the link button to Visible= false or delete the button itself from the template.

Zuhair
  • 699
  • 1
  • 5
  • 12