单击更改图像链接按钮

时间:2013-12-17 07:36:16

标签: c# asp.net image events linkbutton

拥有这段代码:

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

而且:

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

单击链接按钮时如何更改图像。

2 个答案:

答案 0 :(得分:0)

您需要制作img runat="server"并为其分配ID才能在ChangeImage_Click中访问或使用asp:Image代替img

HTML

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

代码bahind

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

答案 1 :(得分:0)

试试这种方式

<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
}