The World’s Leading Microsoft .NET Magazine
   
 
timstall

Donate Today!

Search Box

 

Calendar

««Sep 2008»»
SMTWTFS
 
1
2
3
456
78910111213
14151617181920
21222324252627
282930

My RSS Feeds








Mailing List

Most Popular Tags

                                                           

Anchor onclick vs. Href

posted Monday, 3 October 2005

ASPX, including all its "special" features like postbacks and validators, ultimately renders as standard Html. So it's good to understand how certain Html components, like anchors, work.

A simple anchor just has an target page in the Href, look like so:

<a href="PageB.aspx" >AnchorLink</a>

Clicking this anchor will always redirect to PageB.aspx. The Href property can also contain a method to call. For example, ASP.Net renders LinkButtons as anchors whose Href calls the postback method:

<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>

Anchors can also have an onclick event. You could have this method call a message box, set hidden fields, or do whatever else you needed. However, the Href will not be called if the onclick returns false. For example, you could create a "Do you want to save?" yes-no MessageBox like so:

<a onclick="return confirm('Do you want to save')" href="PageB.aspx">AnchorLink</a>

The JavaScript confirm() method displays a yes-no confirm box, and returns either true or false depending on what the user clicked. Therefore if the user clicks no, the messageBox returns false, and the href is never activated. This concept applies whether the Href contains a navigation page or JavaScript. For example, you could add the confirm box to the LinkButton, and block the postback if the user answered "No" to a certain question.

Knowing the relationship between the anchor onclick and href lets you develop a more interactive and rich UI.

links: digg this    technorati    




1. paul left...
Tuesday, 28 February 2006 3:56 pm

great, to-the-point tip...thanks a bunch!


2. mike left...
Wednesday, 20 September 2006 3:45 pm

thanks for the quick concise help


3. mike left...
Wednesday, 20 September 2006 3:45 pm :: http://radicalbright.com

thanks for the quick concise help


4. James left...
Friday, 6 July 2007 10:07 am

Spot on, just what i was looking 4, thanks!!