【问题标题】:Add New Row in GridView cannot find reference在 GridView 中添加新行找不到参考
【发布时间】:2021-06-21 18:56:51
【问题描述】:

我有一个可编辑的GridView,但我也想添加一个新的行功能,所以我为所有可编辑字段使用FooterTemplate 创建了一个按钮并设置了CommandName="AddNew"。前端看起来一切正常,但 Add New Row 根本找不到 txtGrantIdtxtPotIdtxtBudget 并抛出 System.NullReferenceException 'Object reference not set to an instance of an object.'。问题必须在后面的代码中,但我也附上了前端。数据源很长,但确实调用了OnRowCommand="gvPotsMoneyGrants_RowCommand"

ASPX

<%--Primary Key LinkId--%>
    <asp:TemplateField HeaderText="LinkId" SortExpression="LinkId">
        <ItemTemplate>
            <asp:Label ID="lblLinkId" runat="server" Text='<%# Eval("LinkId") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

<%--GrantId--%>
<asp:TemplateField HeaderText="GrantId" SortExpression="GrantId">
    <ItemTemplate>
        <asp:Label ID="lblGrantIdMain" runat="server" Text='<%#Eval("GrantId") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtGrantId" runat="server" Text='<%#Bind("GrantId") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="inGrantId" Width="120px" runat="server" />
        <asp:RequiredFieldValidator ID="vGrantId" runat="server" ControlToValidate="inGrantId" Text="?" ValidationGroup="VG5" />
    </FooterTemplate>
</asp:TemplateField>

<%--PotId--%>
<asp:TemplateField HeaderText="PotId" SortExpression="PotId">
    <ItemTemplate>
        <asp:Label ID="lblPotIdMain" runat="server" Text='<%#Eval("PotId") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtPotId" runat="server" Text='<%#Bind("PotId") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="inPotId" Width="120px" runat="server" />
        <asp:RequiredFieldValidator ID="vPotId" runat="server" ControlToValidate="inPotId" Text="?" ValidationGroup="VG5" />
    </FooterTemplate>
</asp:TemplateField>

<%--Budget--%>
<asp:TemplateField HeaderText="Budget" SortExpression="Budget">
    <ItemTemplate>
        <asp:Label ID="lblBudgetMain" runat="server" Text='<%#Eval("Budget") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtBudget" runat="server" Text='<%#Bind("Budget") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="inBudget" Width="120px" runat="server" />
        <asp:RequiredFieldValidator ID="vBudget" runat="server" ControlToValidate="inBudget" Text="?" ValidationGroup="VG5" />
    </FooterTemplate>
</asp:TemplateField>

后面的代码

 protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtGrantId");
            TextBox txtPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtPotId");
            TextBox txtBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtBudget");
        string GrantId, PotId, Budget;

        GrantId = txtGrantId.Text;
        PotId = txtPotId.Text;
        Budget = txtBudget.Text;

        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("GrantId", GrantId);
        cmd.Parameters.AddWithValue("PotId", PotId);
        cmd.Parameters.AddWithValue("Budget", Budget);

    }
}

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    请尝试下面的代码。

    protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox inGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inGrantId");
            TextBox inPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inPotId");
            TextBox inBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inBudget");
        string GrantId, PotId, Budget;
    
        GrantId = inGrantId.Text;
        PotId = inPotId.Text;
        Budget = inBudget.Text;
    
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("GrantId", GrantId);
        cmd.Parameters.AddWithValue("PotId", PotId);
        cmd.Parameters.AddWithValue("Budget", Budget);
    
        }
    }
    

    【讨论】:

    • 我错过了 html 元素,它必须是 inBudget 等。非常感谢您指出!
    猜你喜欢
    • 2014-04-23
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多