How to Navigate to a URL on clicking the ASP.NET ImageButton

A user recently on the asp.net forums asked a question on linking the ImageButton to a URL. The requirement was to navigate the user to a URL when the ImageButton was clicked. Here’s how to do so using the OnClientClick()

<asp:ImageButton ID="ImageButton1" runat="server"
src="images/icon260.jpg" onmouseover="this.src='images/emo.jpg'"
onmouseout="this.src='images/icon260.jpg'" CauseValidation="false"
OnClientClick="document.location.href = 'http://www.dotnetcurry.com';
return false" />

Remember to use the OnClientClick() and not the OnClick() event of the ImageButton. The OnClick() event is a server side event.

Alternatively, it’s easier to use a HyperLink control and set it's ImageUrl to an image and set NavigateUrl to the URL you want.

No comments:

Post a Comment