Tuesday, February 19, 2008

[ASP.NET] Difference between HyperLink and LinkButton

Wanna know the difference between the ASP.NET server controls HyperLink and LinkButton?
You're in the right place pal.

The major difference between the two - which will make you choose the one you need - is that The HyperLink will not PostBack your page to the server. It will post a simple request to the server for the URL you set as NavigateURL. The LinkButton works exactly as a normal Button but it looks like an HyperLink, so it will PostBack your page to the server allowing you to do your business (like setting variables at session level or doing some DB operation or whatever). Another (obvious) difference, which is more an imposed consequence of the above, is that the HyperLink doesn't have the OnClick event.

So, if you're asking yourself "which one should I use", here's the answer: if you need to do any operation with data that's on the page you will have to use a LinkButton (or a Button), but if you just need to redirect the user to somewhere else go for an HyperLink (You will avoid half roundtrip!).

E.g. you wanna avoid to have Button or LinkButton handlers with just this code:

Response.Redirect("Whatever.aspx");

which I do all the time, but - hey - I am a certified .NET butcher.

Cheers!

21 comments:

Asad Naeem said...

great and to the point answer.

Anonymous said...
This comment has been removed by a blog administrator.
Unknown said...

very very nive blog to differentiate
thanx buther...brother

Anonymous said...

Great article !
My doubt is clear
Thankx dear.

Andrearound said...

Thanks! great article. Simple and clear!

CVA said...

nice one....keep it up...:-)

Sambasivarao Morla said...

great answer..
exactly ur right.
thanq u.

Sambasivarao Morla said...

great answer.
exactly you are right.
thank you.

tru said...

Thnx 4 d answer

Anonymous said...

Very Well expalined

Anonymous said...

to the point answer
thank you :)

yashwanth said...

Thank U

Mohammed said...

Nice Answer really Thanks to you

kajal said...

very nice answer thanks.....frnds....

Anonymous said...

Great post.The answer was to the point.
Thanx pal.:)

Anonymous said...

Well almost...
The Hyperlink has one unique function that none of the ASP.Net buttons can do - to "target" (open) a second page/tab in the browser, which is very useful and needed in many cases.

Anonymous said...

gr8 ans

Anonymous said...

Thanks

Mike Hoy said...

Your knives are C sharp...

Karuna said...

thank u well said :)

.Net said...

Thanks, explanation good