简体   繁体   English

你调用的对象是空的

[英]Object reference not set to an instance of an object

i am stuck with this error since last few hours.. i dont know what am i doing wrong here.. 自从最近几个小时以来我一直坚持这个错误..我不知道我在这里做错了什么..

    <script type="text/javascript">
    function viewProfile(index)
    {
        var GridID = document.getElementById("PersonGridView");
        var row=GridID.rows[parseInt(index)+1];
        window.open('detailsid.aspx?'+row);
    }
    </script>

        <Columns>
            <asp:BoundField HeaderText="First Name" DataField="FirstName" />
            <asp:BoundField HeaderText="Last Name" DataField = "LastName" />
            <asp:BoundField HeaderText="HomePhoneNumber" DataField="HomePhoneNumber" />
            <asp:TemplateField HeaderText="ViewDetails">
            <ItemTemplate>
            <asp:Button ID="Deatils" runat="server" Text="Details" />
            </ItemTemplate>    

            </asp:TemplateField>

            <asp:TemplateField HeaderText="Actions">
            <ItemTemplate>
            <asp:Button ID="Modify" runat="server" Text="Modify" />
            <asp:Button ID="Delete" runat="server" Text="Delete" />

            </ItemTemplate>
            </asp:TemplateField>


        </Columns>


        <FooterStyle BackColor="#CCCC99" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
</div>
</form>

<p> Code Behind : 
protected void PersonGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

               if (e.Row.RowType == DataControlRowType.DataRow)
            {
         var ID = PersonGridView.DataKeys[e.Row.RowIndex]["ID"].ToString();

                Button btnDetails = (Button)FindControl("Details");
                Button btnModify = (Button)FindControl("Modify");
                Button btnDelete = (Button)FindControl("Delete");
                btnModify.CommandName = "Modify";
                btnDelete.CommandName = "Delete";
                btnDetails.CommandName = "Details";
                btnDelete.CommandArgument = btnModify.CommandArgument = string.Format("{0}", ID);
                btnDetails.Attributes["onclick"] = string.Format("viewProfile({0}); return false;", ID);


            }
        }

Change 更改

        var ID = PersonGridView.DataKeys[e.Row.RowIndex]["ID"].ToString();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

to

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var ID = PersonGridView.DataKeys[e.Row.RowIndex]["ID"].ToString();

ie extract the DataKeys only when the row is of DataRow and not for Header/Footer etc. 即,当行是DataRow而不是Header / Footer等时,才提取DataKeys。

Is this the line that's throwing the error?: 这是抛出错误的那一行吗?:

var ID = PersonGridView.DataKeys[e.Row.RowIndex]["ID"].ToString();

This line is referencing an array item by index in two different places, so you'll need to do a little debugging to determine which one is the problem. 此行在两个不同的位置按索引引用数组项,因此您需要进行一些调试以确定哪个是问题。 Basically, either PersonGridView.DataKeys[] doesn't have an index which matches e.Row.RowIndex or that item doesn't have an index which matches "ID" . 基本上, PersonGridView.DataKeys[]没有与e.Row.RowIndex匹配的索引,或者该项没有与"ID"匹配的索引。 You'll need to either step through a debugger to examine those values at runtime or toss in some debugging code ( Response.Write statements and such to examine values) to see what's going on. 您需要逐步调试调试器以在运行时检查这些值,或者在一些调试代码中抛出( Response.Write语句等来检查值)以查看正在发生的事情。

One common error is that the DataGridView is running this code on non-data rows. 一个常见错误是DataGridView在非数据行上运行此代码。 Header, footer, etc. You can expand the scope of the if statement to address that. 页眉,页脚等。您可以扩展if语句的范围以解决这个问题。 Reference the indexes within the conditional rather than outside of it. 引用条件内的索引而不是它之外的索引。

Edit: (In response to your comment below) 编辑:(回复下面的评论)

Is btnDetails set to an instance of an object? btnDetails是否设置为对象的实例? I'm guessing it isn't. 我猜它不是。 You'll need to continue the debugging and see if the control you're trying to find is actually there. 您需要继续调试并查看您尝试查找的控件是否确实存在。 I notice that it's in a separate ItemTemplate and TemplateField from the other buttons. 我注意到它与其他按钮位于单独的ItemTemplateTemplateField Is that causing an issue perhaps? 这可能导致问题吗? I'm not entirely familiar with these server controls so I'm not sure off-hand. 我对这些服务器控件并不完全熟悉,所以我不确定。

I should take a moment to point out that your code here is very fragile. 我应该花点时间指出你的代码非常脆弱。 The problems you've been experiencing are the result of this. 您遇到的问题就是这样的结果。 You're referencing arrays by indexes manually, including the use of a "magic string" to reference them. 您手动引用索引数组,包括使用“魔术字符串”来引用它们。 You might want to toss in some error checking for that. 你可能想要为此进行一些错误检查。 You're also casting and later using objects without checking if they exist first. 你也在投射并稍后使用对象而不检查它们是否存在。 FindControl can very easily return a null reference, such as when the control isn't found. FindControl可以非常容易地返回null引用,例如在找不到控件时。

Throwing in tons of error checking is kind of part of the reality of using these web forms controls and their old data binding methods. 投入大量错误检查是使用这些Web表单控件及其旧数据绑定方法的现实的一部分。 A lot of extra code, but that's how it goes with these controls and directly binding to weakly-typed DataSets and such. 很多额外的代码,但这就是它如何与这些控件一起使用并直接绑定到弱类型的DataSet等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM