首页 > 解决方案 > 更新面板在 Gridview 中不起作用 - Asp.Net/VB.Net

问题描述

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

在 UpdatePanel“upPnlControls”中找不到触发器的 ID 为“imgPaymentMethod”的控件

前端..

<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

标签: asp.netvb.netgridviewupdatepanel

解决方案


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

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

如果要获取通过imgPaymentMethod按钮单击的网格视图行,则需要使用NamingContainer而不是Parent如下:

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

推荐阅读