We help IT Professionals succeed at work.

Can't hide Edit button on FormView

Eric3141
Eric3141 asked
on
718 Views
Last Modified: 2012-05-10
I try to hide the Edit button on the FormView using the code below.  In the case that the user has the G_CMS_RO role then it does hide the Edit button nicely.  However, for the users who can see the Edit button when they click it they get this error:

{"Object reference not set to an instance of an object."}

I have double checked the ItemTemplate on the FormView and verified that the Edit button is named EditButton.  Any ideas?

protected void FormView2_DataBound(object sender, EventArgs e)
    {
        
        LinkButton lnkbtnEdit = (LinkButton)FormView2.FindControl("EditButton");

        switch (cmsRole)
        {
            //Can Select, Edit, & Delete
            case "G_CMS_Admin":
                lnkbtnEdit.Visible = true;
                break;

            //Can Select & Edit
            case "G_CMS_Users":
                lnkbtnEdit.Visible = true;
                break;

            //Can Select
            case "G_CMS_RO":
                lnkbtnEdit.Visible = false;
                break;

            //Can Select
            default:
                lnkbtnEdit.Visible = false;
                break;
        }
    }

Open in new window

Comment
Watch Question

Top Expert 2013

Commented:
what line is throwing error? Can you post the stack?
Did you set breakpoints and see if you can get some more info.
Also the other code which you thing is relevant.

Author

Commented:
In my GridView if I don't check to see if the object is null first then I can get an error exactly like the one I encountered with the code above.  The error I get with the code above is happening when it gets to case "G_CMS_RO" -- the value of the cmsRole variable is "G_CMS_RO" so it tries to execute that code.  The error says:

"Use the "new" keyword to create an object instance.
Check to determine if the object is null before calling the method.
Object reference not set to an instance of an object."

In my GridView the solution to this sort of error is to add this line of code before the switch statement:

 if (e.Row.FindControl("lnkbtnSelect") != null)
            {...

So, I added a similar line of code (see attached new code)  to the FormView event handler.  Given the error complaining about the possibility of null this seemed reasonable.

Now, however, the switch statement never fires because the expression evaluates to true -- it is null.  And if I change the statement to

if (frmvwCaseNotes.FindControl("lnkbtnEdit") == null)...

then I again get the error above.

I have another page with just a FormView and the code below works great.  On the page with the problem there is a GridView.  User selects a row and then the details are shown in the FormView which is then made visible.  The error above says to check for null before the method is called but not sure where to do that.





protected void frmvwCaseNotes_DataBound(object sender, EventArgs e)
    {
        if (frmvwCaseNotes.FindControl("lnkbtnEdit") != null)
        {
            LinkButton lnkbtnEdit = (LinkButton)frmvwCaseNotes.FindControl("EditButton");

            switch (cmsRole)
            {
                //Can Select, Edit, & Delete
                case "G_CMS_Admin":
                    lnkbtnEdit.Visible = true;
                    break;

                //Can Select & Edit
                case "G_CMS_Users":
                    lnkbtnEdit.Visible = true;
                    break;

                //Can Select
                case "G_CMS_RO":
                    lnkbtnEdit.Visible = false;
                    break;

                //Can Select
                default:
                    lnkbtnEdit.Visible = false;
                    break;
            }

        }

Open in new window

Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I tried the above.  Problem is that in the _Databound event handler lnkbtnEdit is evaluating to true.  I know this because I added a message box right before the "if" statement and told it to tell me.  So the code (pasted here) is not being reached in order to hide the Edit link button on the FormView.  Thoughts?

 //Can Select
            case "G_CMS_RO":
                lnkbtnEdit.Visible = false;
                break;
Top Expert 2013

Commented:
--> Problem is that in the _Databound event handler lnkbtnEdit is evaluating to true.
I am not getting that part. You mean lnkbtnEdit is found or not found? Is it null or not?

Are you sure FormView has item to display? Check what is the value for FormView1.DataItemCount  when you are in DataBound event.

Author

Commented:
I will but I'm confused.  The Edit link button is on the Edit Template for the form.  How could it not be there?  Will do the check you indicated and get back to you soon.

Author

Commented:
OK, FormView1.DataItemCount = 0
Top Expert 2013

Commented:
ok then you need to put a check for which mode the FV is currently in before accessing the button like:

if (FormView1.CurrentMode == FormViewMode.Edit)
 {
        //Then only find the Edit button and hide/show code
}
Top Expert 2013

Commented:
If above does not work then can you share the code of your FV markup.
Also a bit explanation on the scenarios when this button needs to be manipulated and flow of your application for this particular section.
Top Expert 2013

Commented:
--The Edit link button is on the Edit Template for the form.
So when you Select a record from GV to display it in FV, What is the default Mode of FV? ReadOnly or Edit?
Why is your EditLink inside, EditTemplate. I would think it to be in ReadOnly view i.e. itemtemplate.
In the EditTemplate, I would expect Update and Cancel buttons unless your Edit Button is doing the work of Update.

Author

Commented:
My bad.  Edit button is in ItemTemplate.
Top Expert 2013

Commented:
From my understanding the code in my comment#33682956 should work because for the check for null.
Did you try setting breakpoint and see what is happening.  

Author

Commented:
Using the code in the comment you referenced nothing happened because if(lnkbtnEdit!=null) evaluated to "null" instead of "not null".  I used MessageBox to tell me this.  
Top Expert 2013

Commented:
You said FormView1.DataItemCount = 0 correct?
Then if you don't have any item for FormView, there won't be an editlink. So it is giving you null.
As I said, you might want to share some code with some more explanation.

Author

Commented:
--From my understanding the code in my comment#33682956 should work because for the check for null.

Doh!  Doh!  Doh!

I made a dumb mistake.  Upstream in my code I set the value of cmsRole = G_CMS_USERS but in the code (that I pasted originally above) I checked for cmsRole = G_CMS_Users.  Since they are different then, of coure, the link button was not hidden.

I fixed that to make them the same and your code works as posted way up above.  If you lived nearby I'd totally owe you a couple of beers for that!  

Thanks so much man.

Eric
Top Expert 2013

Commented:
Good to know it works. Thanks for beer...unfortunately I don't drink.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.