本文介绍了在Gridview隐藏字段中获取对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在gridview中使用隐藏"字段时,出现对象引用错误.
我在隐藏控件"中绑定了值.如果值一无是处,我会报错.
我也没有检查.但是,代码中仍然会出现错误.
看到我的代码
Hi,
I am getting object reference error when i use Hidden field in gridview.
I bound the value in Hidden Controls. if value is nothing i am getting error.
I have checking Nothing also. But, still getting error in the code.
see my code
<asp:TemplateField HeaderText="Budget ManHours" ItemStyle-Width="65%">
<ItemTemplate>
<asp:HiddenField ID="HdnSal" runat="server" Value='<%# Eval("Sal") %>' />
</ItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtSal" runat="server" ForeColor="Blue" Text='<%# Eval("Sal") %>' Width="75%" ReadOnly="true" AutoPostBack="true" OnTextChanged="txtTot_TextChanged" MaxLength="7"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" ForeColor="Green"></asp:Label>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center"/>
</asp:TemplateField>
ASPX.VB代码
------------
ASPX.VB Code
------------
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chkSelect As CheckBox = TryCast(e.Row.FindControl("chkSelect"), CheckBox)
Dim HdnSal As HiddenField = TryCast(e.Row.FindControl("HdnSal"), HiddenField)
If HdnSal.Value = Nothing Then //Error getting : Object reference if HdnSal is Nothing
chkSelect.Checked = False
Else
chkSelect.Checked = True
End If
End If
推荐答案
If HdnSal.Value = Nothing Then
成为:
If HdnSal Is Nothing OrElse HdnSal.Value Is Nothing Then
这篇关于在Gridview隐藏字段中获取对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!