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:
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- Response.Redirect("~/ForgotPassword.aspx");
- }
Ok so to understand why it’s not a good practice let’s discuss the redirection steps involved when using the mentioned method:
- User submits the page by clicking on the LinkButton control.
- The LinkButton click event is fires which will cause the execution of Respose.Redirect.
- When the server code executes Response.Redirect, it will send redirection request for the browser.
- Browser understand the redirection message and issue a new request for “ForgotPassword.aspx” page.
- 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:
- <asp:HyperLink ID="hypForgotPassword" runat="server" NavigateUrl="~/ForgotPassword.aspx">
- Forgot your password?
- </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