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

A user recently on the 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 = '';
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.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: