Solved

Can't hide Edit button on FormView

Posted on 2010-09-14
16
663 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

0
Comment
Question by:Eric3141
  • 9
  • 7
16 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33677856
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
 
LVL 2

Author Comment

by:Eric3141
ID: 33682637
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33682956
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
 
LVL 2

Author Comment

by:Eric3141
ID: 33683092
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683269
--> 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
 
LVL 2

Author Comment

by:Eric3141
ID: 33683326
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
 
LVL 2

Author Comment

by:Eric3141
ID: 33683443
OK, FormView1.DataItemCount = 0
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683462
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 41

Expert Comment

by:guru_sami
ID: 33683485
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683524
--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
 
LVL 2

Author Comment

by:Eric3141
ID: 33683555
My bad.  Edit button is in ItemTemplate.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683598
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
 
LVL 2

Author Comment

by:Eric3141
ID: 33683809
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683859
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
 
LVL 2

Author Comment

by:Eric3141
ID: 33684026
--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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33684087
Good to know it works. Thanks for beer...unfortunately I don't drink.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now