[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Can't hide Edit button on FormView

Posted on 2010-09-14
16
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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