HyperLink错误:"对象引用未设置为对象的实例"

时间:2014-06-10 19:32:36

标签: asp.net nullreferenceexception

在这个问题上,我已经开始和关闭了近两周的大脑。我无法弄清楚为什么我收到这个错误...任何帮助将不胜感激。

我的代码如下:

   Protected Sub TaxSaleGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles TaxSaleGridView.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim LocationMapHyperLink As HyperLink = CType(e.Row.FindControl("OtherParcel"), HyperLink)


        OPval = DirectCast(e.Row.DataItem, Data.DataRowView)("OtherParcel").ToString()
        PIDval = DirectCast(e.Row.DataItem, Data.DataRowView)("Property_ID").ToString()


        If Len(OPval) > 1 Then

            e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
            LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" & OPval

        Else

            e.Row.Cells(3).BackColor = System.Drawing.Color.YellowGreen
            LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" & PIDval

        End If

    End If

End Sub

这是我的标记:

 <asp:HyperLink ID="LocationMapHyperLink" runat="server"                

    NavigateUrl=""
    Text="Location Map" 
    Target="_blank">

 </asp:HyperLink><br />

这是堆栈跟踪的错误:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 359:                e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
Line 360:
Line 361:                LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" & OPval
Line 362:
Line 363:                LocationMapHyperLin.NavigateUrl = String.Format("http://www.mydomain.com/index.html?parcel{0}", OPval)


Source File: C:\DOTNET\TaxSale\Default.aspx.vb    Line: 361 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   _Default.TaxSaleGridView_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\DOTNET\TaxSale\Default.aspx.vb:361
   System.Web.UI.WebControls.GridViewRowEventHandler.Invoke(Object sender, GridViewRowEventArgs e) +0
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[]     fields, TableRowCollection rows, PagedDataSource pagedDataSource) +306
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4603
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +93
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +17
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +181
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +273
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +104
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +169
   System.Web.UI.Control.EnsureChildControls() +181
   System.Web.UI.WebControls.GridView.get_Rows() +53
   _Default.Page_Load(Object sender, EventArgs e) in C:\DOTNET\TaxSale\Default.aspx.vb:173
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3063

以下是TaxSaleGridview的标记:

<asp:GridView ID="TaxSaleGridView" onrowdatabound="TaxSaleGridview_RowDataBound" 
    runat="server" AutoGenerateColumns="False" 
    DataSourceID="TaxSaleDataDS" CellPadding="4" 
    ForeColor="#333333" PageSize="30" HorizontalAlign="Center" 
    AllowPaging="True" ViewStateMode="Enabled">
    <AlternatingRowStyle BackColor="White" />

    <Columns>        
        <asp:TemplateField HeaderText="Sale ID">            
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxPROPERTYID" runat="server" Text='<%# Bind("PROPERTYID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
            <ItemStyle HorizontalAlign="center" />
                <%Response.Write(startP_bold)%><asp:Label ID="LabelPROPERTYID" runat="server" Text='<%# Bind("PROPERTYID") %>'></asp:Label><%Response.Write(endP_bold)%><br />

                <%If Session(existsOtherParcel) = "N" Then%>
                <asp:Panel ID="pnlNotOnSale1" runat="server">
                <asp:Label ID="lblNotOnSale1" runat="server" Text="***Land is not on sale.***" Font-Bold="True"></asp:Label>
                </asp:Panel>

                <%Else%>

                <%End If%>


            </ItemTemplate>
            <ControlStyle Width="150px" />
            <HeaderStyle Width="150px" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="*Minimum Sale Price" ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxATSALE" runat="server" Text='<%# Bind("ATSALE") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>                                   
                $<asp:Label ID="LabelATSALE" runat="server" Text='<%# Bind("ATSALE") %>'></asp:Label>
            </ItemTemplate>

            

        <asp:TemplateField HeaderText="Property Information and Address" ItemStyle-Width="400">

            <EditItemTemplate>
                <asp:TextBox ID="TextBoxNAME1" runat="server" Text='<%# Bind("NAME1") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelNAME1" runat="server" Text='<%# Bind("NAME1") %>'></asp:Label><br />
                <asp:Label ID="LabelLEGAL_DESC" runat="server" Text='<%# Bind("LEGAL_DESC") %>'></asp:Label><br />
                <asp:Label ID="LabelPROP_ADDR1" runat="server" Text='<%# Bind("PROP_ADDR1") %>'></asp:Label>, 
                <%Response.Write(startZ_bold)%><asp:Label ID="LabelLocation_Zip" runat="server" Text='<%# Bind("Location_Zip") %>'></asp:Label><%Response.Write(endZ_bold)%><br />
                <%Response.Write(startC_bold)%>City: <asp:Label ID="LabelLocation_City" runat="server" Text='<%# Bind("Location_City") %>'></asp:Label><%Response.Write(endC_bold)%><br />                    
                <%Response.Write(startT_bold)%>Taxing District: <asp:Label ID="LabelTax_District_Name" runat="server" Text='<%# Bind("Tax_District_Name") %>'></asp:Label><%Response.Write(endT_bold)%>
            </ItemTemplate>

            

        <%--<asp:BoundField DataField="LEGAL_DESC" HeaderText="LEGAL_DESC" Visible="False" />
        <asp:BoundField DataField="PROP_ADDR1" HeaderText="PROP_ADDR1" Visible="False" />            
        <asp:BoundField DataField="Location_City" HeaderText="Location_City" Visible="False" />
        <asp:BoundField DataField="Location_Zip" HeaderText="Location_Zip" Visible="False" />
        <asp:BoundField DataField="Tax_District_Name" HeaderText="Tax_District_Name" Visible="False" />--%>

        <asp:TemplateField HeaderText="OtherParcel" Visible="true">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxOtherParcel" runat="server" Text='<%# Bind("OtherParcel") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelOtherParcel" runat="server" Text='<%# Bind("OtherParcel") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Reference Materials" ItemStyle-Width="80px" ShowHeader="True" Visible="True">
            <ItemTemplate>
            <!--<%If Session(existsOtherParcel) = "Y" Then%>
            OP exists? <%Response.Write(Session(existsOtherParcel))%><br /><br />
            <%End If%>
            OPval = <%Response.Write(OPval)%><br />
            PIDval = <%Response.Write(PIDval)%><br /><br />-->
                <asp:HyperLink ID="LocationMapHyperLink" runat="server"                

                 NavigateUrl=" "
                 Text="Location Map" Target="_blank" onclick="javascript:window.open(this.href, '','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=780,height=550,left=20,top=20');">

                </asp:HyperLink><br />                  
          <br /> <%'Response.Write(hrefvalue)%> <br /><br />
                <asp:HyperLink ID="PRCHyperLink" runat="server" 
                    NavigateUrl='<%# Eval("UnformattedParcelNumber", "http://www2.hamiltoncounty.in.gov/apps/reports/rptpropcard.asp?sparcelno={0}&stemp=&dSearch=houseno&dreport=propcard&report=&dReportName=Property Card") %>' 
                    Text="Property Record Card" Target="_blank" onclick="javascript:window.open(this.href, '','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=780,height=550,left=20,top=20');">
                </asp:HyperLink>
                <br />
            </ItemTemplate>                
           <ItemStyle Width="80px"></ItemStyle>
        </asp:TemplateField>        
    </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerSettings FirstPageText="First" LastPageText="Last" 
        NextPageText="Next" Position="TopAndBottom" 
        PreviousPageText="Previous" PageButtonCount="1" 
        Mode="NextPreviousFirstLast" FirstPageImageUrl="~/images/misc/first.png" 
        LastPageImageUrl="~/images/misc/last.png" 
        NextPageImageUrl="~/images/misc/next.png" 
        PreviousPageImageUrl="~/images/misc/previous.png" />
    <PagerStyle ForeColor="#333333" HorizontalAlign="Right" Font-Bold="True" Font-Size="Medium" BackColor="#F5F0DC" />
    <RowStyle BackColor="#F5F0DC" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

当我注释掉后面代码的LocationMapHyperLink.NavigateUrl部分时,我得到gridview,显示在OtherParcel列中没有显示空值,并且单元格的背景根据repsective值而变化。

以下是gridview片段的链接: http://i1132.photobucket.com/albums/m561/joelschmidt/taxsalegridview.jpg

1 个答案:

答案 0 :(得分:0)

我强烈怀疑你的e.Row只包含3个单元格,因此它应该是:     e.Row.Cells(2)

请记住:指数从0开始,而不是从1开始。