Can't hide Edit button on FormView

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

LVL 2
Eric3141Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
guru_samiConnect With a Mentor Commented:
You need to make sure that FV has some data before your try finding any control. If there is nothing to display for the FV, it cannot find the editlink button.
For null check you can do like this:

protected void FormView2_DataBound(object sender, EventArgs e)
    {
        LinkButton lnkbtnEdit = (LinkButton)FormView2.FindControl("EditButton");
     
        if(lnkbtnEdit!=null)
        {
        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;
        }
      }
    }
Note: You should always a null check when you are finding a control and then use it.
0
 
guru_samiCommented:
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.
0
 
Eric3141Author 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

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Eric3141Author 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;
0
 
guru_samiCommented:
--> 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.
0
 
Eric3141Author 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.
0
 
Eric3141Author Commented:
OK, FormView1.DataItemCount = 0
0
 
guru_samiCommented:
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
}
0
 
guru_samiCommented:
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.
0
 
guru_samiCommented:
--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.
0
 
Eric3141Author Commented:
My bad.  Edit button is in ItemTemplate.
0
 
guru_samiCommented:
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.  
0
 
Eric3141Author 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.  
0
 
guru_samiCommented:
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.
0
 
Eric3141Author 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
0
 
guru_samiCommented:
Good to know it works. Thanks for beer...unfortunately I don't drink.
0
All Courses

From novice to tech pro — start learning today.