更新面板在 Gridview 中不起作用 - Asp.Net/VB.Net

Update panel not working inside Gridview - Asp.Net/VB.Net

当我单击图像按钮时 'imgPaymentMethod' 我需要代码来更改图像按钮的 ImageUrl。但是,单击时没有任何反应。我还尝试将整个 gridview 放入更新面板,但随后出现此错误:

A control with ID 'imgPaymentMethod' could not be found for the trigger in UpdatePanel 'upPnlControls'

前端..

<asp:GridView ID="gdvItems" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="fileID" DataSourceID="DSUploadedItems" CssClass="mGrid" 
                    AlternatingRowStyle-CssClass="alt">
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
    <Columns>
        <asp:TemplateField visible="true" HeaderText="Price">
            <ItemTemplate>    
                <asp:DropDownList ID="ddlBuyPrice" runat="server" AutoPostBack="True" Font-Size="11px" Height="22px" OnSelectedIndexChanged="ddlBuyPrice_SelectedIndexChanged" SelectedValue='<%# Bind("buyPrice")%>'>
                     <asp:ListItem Value="0.00">Select:</asp:ListItem>
                </asp:DropDownList>
                <asp:LinkButton visible="false" ID="lnkPaymentMethod" runat="server" CausesValidation="False"
                    Text='<%# Bind("acceptPaypal")%>' > /></asp:LinkButton>
                <br /><br />
                <asp:Label ID="lblSoldStatus" visible="false" runat="server" CausesValidation="False" Text='<%# Bind("sold") %>' />
                <asp:Image ID="imgSoldStatus" alt="Sold Status" runat="server" width="100px" ImageUrl="~/files/images/icons/iconSold.png" />
                <br />

                <asp:UpdatePanel ID="upPnlControls" style="margin-top:0px;" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
                    <Triggers>
                        <asp:AsyncPostBackTrigger controlid="imgPaymentMethod" eventname="Click" />
                    </Triggers>
                    <ContentTemplate>
                        <asp:ImageButton visible="true" ID="imgPaymentMethod" runat="server" CausesValidation="False" width="50px" onclick="lnkPaymentMethod_Click"></asp:ImageButton>
                    </ContentTemplate>
                </asp:UpdatePanel>

                <br />
                <asp:Label ID="lblDateBought" runat="server" CausesValidation="False" Text='<%# Bind("dateBought") %>' />
                <asp:LinkButton ID="lnkMarkAsSold" runat="server"  OnClick="markAsSold_Click" Text="Mark As Sold" Visible="true" ></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

VB代码..

Protected Sub lnkpaymentMethod_Click(sender As Object, e As System.EventArgs)
    Dim imgPaymentMethod As ImageButton = CType(sender, ImageButton)
    Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)

    If imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif" Then
        imgPaymentMethod.ImageUrl="~/files/images/icons/cashIcon.gif"
    Else
        imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif"
    End If

End Sub

经过我们在评论中的讨论,如果我理解正确的话,这里的问题是这一行:

Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)

如果你想获得通过imgPaymentMethod按钮点击的网格视图行,你需要使用NamingContainer而不是Parent,如下所示:

Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.NamingContainer, GridViewRow)