0

I am trying to hide (or show) an EDIT commandfield button on my GridView. To do this, I am following the example here (Conditionally hide CommandField or ButtonField in Gridview) which worked great. I converted the field to a templatefield, named the button, and sure enough it works (hides/shows as expected). YAY!

One problem though. When I click on the EDIT button, I do not go into edit mode, and I get an error actually. The error is, "Object Reference not set to an instance of the object". This is on the line that reads, "btnEdit.Visible = false;"

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Hide the edit button when some condition is true
            // for example, the row contains a certain property
            if (CanEdit == false)
            {
                System.Web.UI.WebControls.LinkButton btnEdit = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("btnEdit");
                btnEdit.Visible = false;
            }
            else
            {
                System.Web.UI.WebControls.LinkButton btnEdit = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("btnEdit");
                btnEdit.Visible = true;
            }
        }

I did have another seperate technique to hide the EDIT button which was to use .Controls.Clear() when a condition was met in the RowDataBound event. That complete line is, "e.Row.Cells[e.Row.Cells.Count - 1].Controls.Clear();" The problem is, when I click on the EDIT button an dgo into EDIT mode, the UPDATE and CANCEL button's are not there. I asusme they were cleared in the previous command.

        if (CanEdit == false)
        {
            //disble row editing
            //e.Row.Cells[e.Row.Cells.Count - 1].Controls.Clear();

        }

So my question can be one or the other. How can I convert the field to a template field, make the edit button visible and go into edit mode when the button is clicked. OR, how can I use hte .Controls.Clear() method, but clear only the edit button?

Thank you so much!

Community
  • 1
  • 1
Rob
  • 33
  • 1
  • 1
  • 5
  • Where do you run the code that makes the button visible or not? In which GridView event? – Tasos K. Feb 12 '14 at 18:58
  • @codingstill, the button is visible by default, and both techniques referenced above occur in the Gridview RowDataBound event. – Rob Feb 13 '14 at 12:50
  • I tried to reporduce the error but without success. One thought: Do you have the if(!Page.IsPostBack) {...} when you bind the GridView in the Page_load(...) event? – Tasos K. Feb 13 '14 at 13:49
  • The only thing in my PageLoad secion, postback is the following, which executes a SQL stored procedure and loads the results into a text box. Would I need to put something there for my current problem? `if (!IsPostBack) { txtSessionNotes.Text = ToolBox.GetSessionNotes(); }` – Rob Feb 13 '14 at 14:50
  • Try to put the code that binds the GridView inside the if(!Page.IsPostBack) {...} – Tasos K. Feb 13 '14 at 14:54
  • So I should create a new method with all the code currently in the RowDataBound event, and call that method from the `if postback` in the PageLoad event? I'll give that a shot EDIT: One issue I run into right away is 'Gridview1 does not exist in current context' error pops up, as well as my event row handler (e.) generating the same error. I also get the same set of errors if I just move the code, without creating a new method, inside the `if post back` section. – Rob Feb 13 '14 at 15:20
  • No, I mean the `GridView1.DataBind();` call. – Tasos K. Feb 13 '14 at 16:23
  • When I put it in Gridview1.DataBinding() I get many 'errors' now that the variable 'e' has changed. Using 'e' allowed me to get values from the gridview before under , but I can't seem to get the same data by using 'Gridview.Rows.Cells[0].text' as an example under Gridview1.RowDataBound() – Rob Feb 17 '14 at 18:56

0 Answers0