Ravikumar Ravikumar - 1 year ago 79
ASP.NET (C#) Question

How to change image source inside LinkButton in Repeater Asp.Net C#

I am trying to get image inside LinkButton in Repeater and wanted to change image Src but it does not changing image UI side.

HTMl Code:

<asp:Repeater ID="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand" >
<HeaderTemplate>
<table c>
<tr>
<th>
<asp:LinkButton ID="lbtnC1" CommandName="Col1" runat="server">Column1 <img id="imgStamp" runat="server" src="~/arrow-up-down.gif" alt="Asc" style="vertical-align:middle;padding-left:3px" /></asp:LinkButton>
</thalign>
<th>
<asp:LinkButton ID="lbtnC2" runat="server"
CommandName="Col2">Column2 <img src="~/arrow-up-down.gif" alt="Asc" style="vertical-align:middle;padding-left:3px; visibility:hidden;" /></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# Event

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");
}
}

Answer Source

By doing this i'm able to solve my issue.

protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    Image imgStamp = Repeater.Controls[0].Controls[0].FindControl("imgStamp") as Image;
    imgStamp.ImageUrl = Page.ResolveUrl("URL");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download