[英]How to change image source inside LinkButton in Repeater Asp.Net C#
我试图在Repeater的LinkButton中获取图像,并想更改图像Src,但它不会更改图像UI端。
HTMl代码:
<asp:Repeater ID="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand" >
<HeaderTemplate>
<table c>
<tr>
<th>
<asp:LinkButton ID="lbtnC1" CommandName="Col1" runat="server">Column1 <asp:Image id="imgStamp" ClientIDMode="AutoID" runat="server" style="vertical-align:middle;padding-left:3px" /></asp:LinkButton>
</thalign>
<th>
<asp:LinkButton ID="lbtnC2" runat="server"
CommandName="Col2">Column2 <asp:Image id="imgStamp" ClientIDMode="AutoID" runat="server" style="vertical-align:middle;padding-left:3px" /></asp:LinkButton></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="C1Value" runat="server" />
</td>
<td>
<asp:Literal ID="C2Value" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
C#事件
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton linkButton = e.CommandSource as LinkButton;
HtmlImage image = linkButton.Controls[0] as HtmlImage;
if (e.CommandName == "Col1")
{
image.Src = Page.ResolveUrl("~/arrow-down-white.gif");
}
}
将其更改为HtmlImage image = linkButton.Controls[1] as HtmlImage;
我相信(但不是100%确信)第一个控件是Literal。
通过这样做,我能够解决我的问题。
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Image imgStamp = Repeater.Controls[0].Controls[0].FindControl("imgStamp") as Image;
imgStamp.ImageUrl = Page.ResolveUrl("URL");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.