0

I have the error below when I tried to hide the delete button from the gridview when the user is not admin. "Additional information: Object reference not set to an instance of an object"

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ClinicalFollowUpID"
  OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"                            
  AllowPaging="True" OnPageIndexChanging="OnPaging" PageSize="5"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting">
<Columns>
  <asp:GridView ID="GridView1" runat="server AutoGenerateColumns="False" DataKeyNames="ClinicalFollowUpID"
      OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing"   OnRowCancelingEdit="OnRowCancelingEdit"
  OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting">
 <Columns>
  <asp:TemplateField HeaderText="ID" Visible="false">
   <ItemTemplate >
     <asp:Label ID="lblClinicalFollowUpID" runat="server" Text='<%# Eval("ClinicalFollowUpID") %>' >
    </asp:Label>
   </ItemTemplate>

</asp:TemplateField>
       <asp:TemplateField HeaderText="MBID">
       <ItemTemplate >
  <asp:Label ID="lblMBID" runat="server" Text='<%# Eval("MBID") %>' >
      </asp:Label>
           </ItemTemplate>
             </asp:TemplateField>
   </asp:TemplateField>
   <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" HeaderText="Click to Edit">
        <ItemStyle Width="150px"></ItemStyle>
         </asp:CommandField>
        </Columns>

C# Code

  protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        lbltype.Text = Session["Type"].ToString();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (lbltype.Text != "admin")
            {

               LinkButton lnkedit = (LinkButton)GridView1.FindControl("lnkedit");
                lnkedit.Visible = false;
            }
        }
  }
Eric Mbiada
  • 133
  • 1
  • 11
  • Please check your gridview HTML and see if there is any linkbutton named "lnkedit". – Suprabhat Biswal Jun 12 '17 at 11:28
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD Jun 12 '17 at 11:53
  • Why not add an ItemTemplate so you can have more control [how-to-disable-a-control-in-command-field-control-in-gridview](https://stackoverflow.com/questions/7187879/how-to-disable-a-control-in-command-field-control-in-gridview) – roy.d Jun 12 '17 at 12:10

2 Answers2

1

You won't be able to access the link button using FindControl unless you define the template manually after turning off the AutoGenerateEditButton property of the grid view.

Try the following to find the edit link button and hide it (assuming the last column corresponds to the command field):

if (e.Row.RowType == DataControlRowType.DataRow)
  {
     if (lbltype.Text != "admin")
       {
         LinkButton deleteLink = (LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[2];
         if(deleteLink != null && deleteLink.CommandName.Equals("Delete"))
           {
              deleteLink.Visible = false;
           }
       }
  }
ashin
  • 2,543
  • 1
  • 12
  • 17
  • Hi Ashin thanks for your help the code is working but instead of hiding the delete button it's hiding the edit button any help how to solve the issue. Thanks – Eric Mbiada Jun 12 '17 at 12:46
  • Updated the answer..just have to get the second item in the control array – ashin Jun 12 '17 at 14:16
  • sorry, i have updated the answer yet again..you need to use the following code: LinkButton deleteLink = (LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[2]; deleteLink.Visible = false; – ashin Jun 12 '17 at 17:25
  • Hi Ashin Thanks for you advise.The code now hide the delete button but the issue I have now is when you click on Edit button instead of showing the Update and Cancel button is only the update button that is visible therefore a user cannot cancel an update if they want to. Any help how to solve the issue. Thanks – Eric Mbiada Jun 13 '17 at 07:51
  • please check the updated answer..That should fix the issue. I will provide a better way to tackle this..just give me some time – ashin Jun 13 '17 at 07:55
  • Thanks Ashin Worked great. I will accept the answer. If you have a better solution please feel free to add – Eric Mbiada Jun 13 '17 at 09:59
0

try this

       Button btnEdit = (Button)e.Row.FindControl("Link");
        btnEdit.Visible = false;

or

       if (e.Row.RowType == DataControlRowType.DataRow )
        {
         var editBtn=   e.Row.Cells[3].Controls[2] as Button;
         editBtn.Visible = false;

        }
Abinash
  • 471
  • 3
  • 13