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.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: