簡體   English   中英

在 GridView 中添加新行找不到引用

[英]Add New Row in GridView cannot find reference

我有一個可編輯的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);

    }
}

請嘗試下面的代碼。

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);

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM