Change image linkbutton on click

1.4k views Asked by At

Having this piece of code:

<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
  <img src="imagethatshouldbechanged" />
</asp:LinkButton>

And this:

protected void ChangeImage_Click(object sender, EventArgs e)
{
    //Code here
}

How do I change the image when I click the linkbutton.

2

There are 2 answers

0
Adil On BEST ANSWER

You need to make img runat="server" and assign id to it to access in ChangeImage_Click or use asp:Image instead of img

HTML

<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
  <img id="img1" runat="server" src="imagethatshouldbechanged" />
</asp:LinkButton>

Code bahind

protected void ChangeImage_Click(object sender, EventArgs e)
{
    img1.Src = "url";
}
0
Ramesh Rajendran On

Try this way

<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
  <img runat="server" id="myImage" src="imagethatshouldbechanged" />
</asp:LinkButton>

protected void ChangeImage_Click(object sender, EventArgs e)
{
   myImage.Src="YourImagePath";//Like App/app1/image1.jpeg
}