asp.net - 更新面板在 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
解决方案
经过讨论,我们在评论中进行了讨论,如果我理解正确,这里的问题是这一行:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
如果要获取通过imgPaymentMethod
按钮单击的网格视图行,则需要使用NamingContainer
而不是Parent
如下:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.NamingContainer, GridViewRow)
推荐阅读
- python-3.x - 使用mianloop()时如何避免关闭Mac的屏幕
- ldap - 使用 ldapjs 节点库在 LDAP 中控制和分页 ldap_search
- java - java - 如何使用Java中的websockets从服务器端点类调用方法?
- javascript - 单击复选框后,addEventListener 不起作用
- laravel - 在 Laravel (windows) 中创建符号链接
- javascript - 用于重复模式的正则表达式
- sql-server - SQL Server 2017 - 数据库还原,无需替换即可覆盖
- partition - MySQL 对存档表进行分区的最佳方法是什么?
- android - Fragment 布局的一部分未在 Activity 中显示
- react-native - 谁能告诉我,如何在 react-native 的平面列表中使用图像?