0

我有以下aspx代码;

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductId"
DataSourceID="edsinventory" OnRowCommand="GridView1_RowCommand"
ShowFooter="true" CssClass="mGrid">

    <Columns>
      <asp:TemplateField ShowHeader="False">
        <EditItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update"></asp:LinkButton>
            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
        <FooterTemplate><asp:LinkButton ID="LinkButton3" runat="server" CommandName="Insert">Insert</asp:LinkButton></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>
    <asp:TemplateField HeaderText="Id" InsertVisible="False" 
        SortExpression="Id">
        <EditItemTemplate>
            <asp:Label ID="lblEditId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductId" SortExpression="ProductId">
        <EditItemTemplate>
            <asp:TextBox ID="tbProdId" Width="50" runat="server" Text='<%# Bind("ProductId")%>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="tbInsertProdId" Width="50" runat="server"></asp:TextBox>
        </FooterTemplate>
        <ItemTemplate>
            <asp:Label ID="lblProdId"  runat="server" Text='<%# Bind("ProductId")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    'shortened for brevity



    </Columns>
</asp:GridView>

这是后面的代码

 Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    ' This just provides easier access to the Cells in the GridView
    Dim cells = GridView1.FooterRow.Cells
    Dim ctx As New teckEntities()
    Dim addInventory As New inventory()

    If e.CommandName = ("Insert") Then
        'Create new Inventory object
        addInventory.ProductId = Convert.ToInt32(DirectCast(cells(3).FindControl("tbInsertProdId"), TextBox).Text)
        addInventory.Quantity = Convert.ToInt32(DirectCast(cells(4).FindControl("tbInsertQuantity"), TextBox).Text)
        addInventory.Location = Convert.ToString(DirectCast(cells(1).FindControl("tbInsertLocation"), TextBox).Text)
        'attach the fields to the inventory context
        ' note: Id autoincrements and doesn't need to be set manually

        InsertInventoryItem(addInventory)
        'need to call a gridview refresh here
        GridView1.DataBind()
    ElseIf e.CommandName = "Delete" Then

        **addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)**

        DeleteInventoryItem(Convert.ToInt32(addInventory.Id))
        GridView1.DataBind()
    End If

End Sub

我在删除例程中出现以下错误**,为什么?插入功能以相同的方式工作并且正常工作。你调用的对象是空的。

4

1 回答 1

0

我认为问题在于您正在查看页脚控件中的 Label lblEditId:

Dim cells = GridView1.FooterRow.Cells
...
addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)

试试这个:

将删除链接按钮更新为(将 ID 添加到 CommandArgument):

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"  CommandArgument='<%#Eval("Id")%>' Text="Delete"></asp:LinkButton>

然后,将您的 ** 代码替换为: 注意:根据您提供的代码,lblEditId 似乎在第 2 列中,因此 Cell(1) - 基于零的索引,而不是 Cell(3)。

addInventory.Id = Convert.ToInt32(DirectCast(GridView1.Rows(e.CommandArgument).Cells(1).FindControl("lblEditId"), Label).Text)
于 2012-10-02T18:02:45.547 回答