當我試圖在用戶不是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;
}
}
}
請檢查你的GridView的HTML,看看是否有一個名爲「lnkedit」任何的LinkButton 。 – Prabhat
[什麼是NullReferenceException,我該如何解決它?]可能的重複(https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ) – VDWWD
爲什麼不添加一個ItemTemplate,所以你可以有更多的控制 [如何禁用控制在命令字段控制在網格視圖](https://stackoverflow.com/questions/7187879/)如何禁用一個控制在命令字段控制在網格視圖) –