Ravikumar
Ravikumar

Reputation: 625

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

Upvotes: 0

Views: 525

Answers (2)

Ravikumar
Ravikumar

Reputation: 625

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

Upvotes: 0

VDWWD
VDWWD

Reputation: 35514

Change it to HtmlImage image = linkButton.Controls[1] as HtmlImage;

I believe (but not 100% sure) that the first control is a Literal.

Upvotes: 1

Related Questions