[英]Object not set to an instance of an object
I've been scratching my head about this for about a day now and need some help. 我已经为此花了大约一天的时间挠头,需要一些帮助。 I have a GridView and I would like to change the back-color of the row based on a database field.
我有一个GridView,并且我想根据数据库字段更改行的背景色。 The db field is "Inactive".
db字段为“无效”。
Here is the markup: 这是标记:
<asp:GridView ID="GridView1" runat="server" DataSourceID="WishListDS" AutoGenerateColumns="false" CssClass="WishListGridView" GridLines="None" OnRowDataBound="WishListGV_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="wlMessage">
<asp:Hyperlink ID="ViewHL" runat="server" Text="View" NavigateUrl='<%# "WishListSearchResults.aspx?id=" + Eval("sysid")%>' />
<asp:Hyperlink ID="EditHL" runat="server" Text="Edit" NavigateUrl='<%# "WishListEdit.aspx?id=" + Eval("sysid")%>' />
</div>
<asp:Hyperlink ID="NameLBL" Runat="server" Text='<%# Eval("customName")%>' NavigateUrl='<%# "WishListSearchResults.aspx?id=" + Eval("sysid")%>' CssClass="wlGVContentTitle" />
<asp:Label ID="ArrivalLBL" Runat="server" Text='<%# Eval("earliestArrival","{0:d}") + " - " + Eval("latestArrival","{0:d}")%>' CssClass="wlGVContent" />
<asp:Label ID="StateLBL" Runat="server" Text='<%# Eval("City") + ", " + Eval("State")%>' CssClass="wlGVContent"></asp:Label>
<asp:HiddenField ID="InactiveHF" runat="server" value='<%# Eval("InActive") %>' />
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is the code: 这是代码:
protected void WishListGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hf = (HiddenField)e.Row.FindControl("InActiveHF");
if (hf.Value == "True")
{
}
}
}
I get an error: 我收到一个错误:
Object reference not set to an instance of an object."
你调用的对象是空的。”
an the line 一线
if (hf.Value == "True")
Anyone have any ideas on why this is happening? 有人对为什么会这样有任何想法吗?
There is mismatch between HiddenField's Id in markup and in code. 标记和代码中的HiddenField的ID不匹配。 Use this:
用这个:
HiddenField hf = (HiddenField)e.Row.FindControl("InactiveHF");
In common case, to prevent NullReferenceException, check against null: 通常,为了防止NullReferenceException,请检查是否为null:
HiddenField hf = (HiddenField)e.Row.FindControl("id");
if (hf != null && hf.Value == Boolean.TrueString)
{
}
else
{
// handle on your own, e.g.:
throw new InvalidOperationException("Control not found");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.