Friday, December 2, 2011

How to Redirect or Send a User from Current Page to Another Page



Of course we can use the traditional way of Html redirection in asp.net web forms but some times you need a more controlled redirection. For instance, you want to validate a user input using the server side code before you send the user to the desired page. At this time you need a kind of, let me say, deferred redirection. There are a number of alternatives to do this and that in asp.net web forms.

We can use the html anchor tag as usual to make unconditional redirection meaning that the user once he/she clicks the link is directly redirected or sent to the navigation url.

    <a href="Order.aspx" target="_blank"> Go to Order</a>
Asp.net has a similar control known as HyperLink
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Order.aspx">Go to Order</asp:HyperLink>

We can also use the PostBackUrl property of button controls ( Button, ImageButton, & LinkButton)
So you can set this property directly in the control at design time or inside code file at runtime.
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Default.aspx">Go to Home</asp:LinkButton>

Or in the LinkButton1 click event or in any other event of the page and its controls just like this:
 
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles LinkButton1.Click
        ' your other code goes here
        LinkButton1.PostBackUrl = "~/Default.aspx"
    End Sub

Look at the above controls carefully. You’ll notice that the first two controls used the NavigateUrl property while the button controls used the PostBackUrl property. Yes there is a reason why these controls use different properties though they are all meant to send the user to another page. The reason is the above controls (<a> html tag and the HyperLink asp.net control) do not cause the current page to be sent to the server while PostBackUrl, as the name suggests, first send the current page to the server and then, if the server code doesn’t stop the redirection, sends the user to the specified url (page). If you are asking what is the purpose of posting back the current page to the server, the answer can be sometimes you may need to have control over the redirection that is you may have some content to submit first before sending the user to another location. For example, if you are validating form input in server code you may need to stop the redirection and send error messages back to the current page. In other terms, anchor tag and HyperLink control do not cause validation that means the user skips the validation errors and is able to jump to the url specified by these tags while button controls cause validation and will not allow the user to navigate away from the current page (Note: you can stop buttons from causing validation by setting the CausesValidation property to false).

Request.Redirect("url") and Server.Transfer("url") are methods that made possible to redirect users without the need of any tag or asp.net control. Request and Respond are properties of the Page class. These properties will enable us redirect users at any time in our code. For example, we will redirect a user directly to another page before the user can see the page he/she requests.
Put this line of code in one of the pages events that occur before the page is rendered to the browser:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Response.Redirect("~/Order.aspx")
End Sub
Or put the statement in the Page.Load event after you perform some operations. Remember in both events you will not be able to see the requested page (i.e: if you request http://localhost/MyWebsite/default.aspx and you place that code in some event of the default page you will never be seeing the default.aspx web form)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim IMG As Image = CType(Master.FindControl("Image1"), Image)
        IMG.ImageUrl = "~/RememberAllah.png"
        Response.Redirect("~/Order.aspx")
End Sub

No comments:

Post a Comment

Search This Blog (raadi)