Null Exception在代码隐藏中声明gridview

时间:2015-09-25 19:07:22

标签: c# asp.net gridview

部分问题源于在FormView中嵌入GridView并在GridView控件上运行SQL。

GirdView标记:

<asp:GridView ID="gvBFProd" runat="server" ShowFooter="True" AutoGenerateColumns="False" DataKeyNames="BFProdID" DataSourceID="BFProdSQL" OnRowCommand="gvBFProd_RowCommand">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Save"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="ButtonAdd" runat="server" CommandName="FooterInsert" Text="Insert"  />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
...

代码隐藏

private GridView gvBFProd;
    protected void gvBFProd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        gvBFProd = FormView1.Row.FindControl("gvBFProd") as GridView;

        if (e.CommandName == "FooterInsert" && Page.IsValid)
        {
            // Reference GridView Controls
            TextBox txtBFID = (TextBox)FormView1.Row.FindControl("txtBFID");
            DropDownList ddlSeries =
                (DropDownList)gvBFProd.FooterRow.FindControl("ddlFooterSeries");
            TextBox txtACMWelders =
                (TextBox)gvBFProd.FooterRow.FindControl("txtFooterACMWelders");
....                

// Update InsertParameters
            BFProdSQL.InsertParameters.Clear();

            BFProdSQL.InsertParameters.Add("BFID", txtBFID.Text);
            BFProdSQL.InsertParameters.Add("SeriesID", ddlSeries.SelectedValue);
            BFProdSQL.InsertParameters.Add("NumACMWelders", txtACMWelders.Text);
...         


// Insert new record
            BFProdSQL.Insert();
        }
    }

由于嵌入在FormView中,初始private GridView gvBFProd;是为GridView控件引用提供上下文所必需的。但是,要么某些东西没有正确传递,要么我需要对private GridView gvBFProd;行做更多的事情。我认为后者一旦项目建成,我就会在gvBFProd下得到绿色的波浪形状,说明该字段从未被分配,并且将始终将其默认值设置为null。当然,当我单击Insert LinkBut​​ton时,我得到一个未处理的Null Exception错误,该错误在(DropdownList)gvBFProd.FooterRow....行处中断。

我是否需要为gvBFProd构建get / set功能,或者有不同的方式传递GridView?

1 个答案:

答案 0 :(得分:0)

找到导致原始Null异常错误的原因。我在代码隐藏中错过了gvBFProd的定义。

具体做法是:

private GridView gvBFProd;
    protected void gvBFProd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        gvBFProd = FormView1.Row.FindControl("gvBFProd") as GridView;
....

现在我在下拉列表中得到了一个不同的Null Reference Exception,但这是一个不同的问题。