2017-06-12 86 views
0

當我試圖在用戶不是admin時從gridview中隱藏刪除按鈕時,出現以下錯誤。 「其他信息:對象引用不設置到對象的實例當我試圖隱藏GridView上的刪除按鈕時出現錯誤

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#代碼

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; 
      } 
     } 
    } 
+0

請檢查你的GridView的HTML,看看是否有一個名爲「lnkedit」任何的LinkBut​​ton 。 – Prabhat

+0

[什麼是NullReferenceException,我該如何解決它?]可能的重複(https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ) – VDWWD

+0

爲什麼不添加一個ItemTemplate,所以你可以有更多的控制 [如何禁用控制在命令字段控制在網格視圖](https://stackoverflow.com/questions/7187879/)如何禁用一個控制在命令字段控制在網格視圖) –

回答

1

您將無法除非您關閉網格視圖AutoGenerateEditButton屬性財產後手動定義模板訪問使用的FindControl鏈接按鈕。

請嘗試以下方法找到編輯鏈接按鈕,隱藏它(假設最後一欄對應的命令字段):

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; 
      } 
     } 
    } 
+0

嗨Ashin感謝您的幫助代碼工作,而不是隱藏刪除按鈕它隱藏編輯按鈕任何幫助如何解決問題。謝謝 –

+0

更新了答案..只好得到控制數組中的第二項 – ashin

+0

對不起,我已經更新了答案再次..你需要使用下面的代碼:LinkBut​​ton deleteLink =(LinkBut​​ton)e.Row.Cells [ e.Row.Cells.Count - 1] .Controls [2]; deleteLink.Visible = false; – ashin

0

試試這個

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

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

     } 
相關問題