Tip of the day: Don’t misuse the Link button control

Misuse ? Yes it is !

I have seen a lot of developers who are using the LinkButton to do redirection only !

They are handling it’s click event to just write Response.Redirect("url”) like this:

  1. protected void LinkButton1_Click(object sender, EventArgs e)
  2. {
  3.     Response.Redirect("~/ForgotPassword.aspx");
  4. }

Ok so to understand why it’s not a good practice let’s discuss the redirection steps involved when using the mentioned method:

  1. User submits the page by clicking on the LinkButton control.
  2. The LinkButton click event is fires which will cause the execution of Respose.Redirect.
  3. When the server code executes Response.Redirect, it will send redirection request for the browser.
  4. Browser understand the redirection message and issue a new request for “ForgotPassword.aspx” page.
  5. Server receive the request and send back the “ForgotPassword.aspx” to the browser.

As you can see, using Response.Redirect involves a lot of requests which can be avoided if you only use a HyperLink control like this:

  1. <asp:HyperLink ID="hypForgotPassword" runat="server" NavigateUrl="~/ForgotPassword.aspx">
  2. Forgot your password?
  3. </asp:HyperLink>

This way, when the user click “Forgot password?” link, the browser will just request the “ForgotPassword.aspx” page,Hence it will skip step 1,2,3 and just go through step 4 and 5.

 

Summary:If you want to do redirection without doing any server side calculation, you can avoid all of the mentioned  extra requests by using the HyperLink control instead of the LinkButton or the standard Button control.

 

Hope it helps.

Anas

Shout it
Published Tuesday, April 20, 2010 1:09 AM by anas
Filed under:

Comments

# re: Tip of the day: Don’t misuse the Link button control

Tuesday, April 20, 2010 12:19 AM by Arun Mahendrakar

Plus, Response.Redirect will cause another round trip to the server!

# re: Tip of the day: Don’t misuse the Link button control

Tuesday, April 20, 2010 10:51 AM by Phil

You are missing a fundamental point. A business requirement may be that there be a linkbutton that redirects to a new page. Yeah, you do not neccessarily have to use a linkbutton; but you have to use something to meet the business requirement...right? Why else would a developer do this? You can try CSS to play with the background to make it look like a linkbutton (and I have done this); but you lose out in the button-feel. What do you recommend in the case where a page-redirect linkbutton-like feature is specifically request?

# re: Tip of the day: Don’t misuse the Link button control

Tuesday, April 20, 2010 1:37 PM by anas

@phil

I'm not sure if i understand you.I'm not sure how this related to business requirment ?

What i'm saying in this post is that if you just need to redirect the user to a new page and without the need to execute server side code, then you should use the HyperLink control instead of the controls that causes the postback like LinkButton or the Button control.

# re: Tip of the day: Don’t misuse the Link button control

Wednesday, June 15, 2011 9:01 AM by chandradev1

It's really useful tip.