Sunday, October 2, 2005

Anchor onclick vs. Href

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/anchor_onclick_vs_href.htm]

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:

AnchorLink

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:

href="javascript:__doPostBack('LinkButton1','')">LinkButton

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:

AnchorLink

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.

3 comments:

  1. Jiangsu yaxing anchor chain co., LTD. (AsAc) is a professional engaged in Marine cable and Marine mooring chain production enterprise, and it is China's Marine cable and anchor chain factoryproduction and export base, is the world's one of the largest in the industry, the most has the comprehensive strength of the modern enterprise.

    ReplyDelete
  2. Jiangsu yaxing chain co., LTD. (AsAc) is a professional engaged in Marine cable and Marine mooring chain production enterprise, and it is China's Marine cable and Marine mooring chain production and export base, is the world's one of the largest in the industry, the most has the comprehensive strength of the modern enterprise.

    ReplyDelete
  3. Jiangsu yaxing anchor chain co., LTD. (AsAc) is a professional engaged in Marine cable and Marine mooring chain production enterprise, and it is China's Marine cable and Marine studless anchor chain production and export base, is the world's one of the largest in the industry, the most has the comprehensive strength of the modern enterprise.

    ReplyDelete