locked
capturing an action when button is clicked RRS feed

  • Question

  • User-309740890 posted

    What is a good way:

     

    I have page1.aspx contains (Next button), it redirects to Page2.aspx. Page2.aspx has a back button that redirects to page1.aspx

     

    I want to capture the action if page2.aspx (back button was clicked) and it made it redirect to page1.aspx page.

     

    Reason is because if the back button is clicked at   if (!IsPostBack) {...} I will reload the data that was entered when going from page1 to page2.

     

    I know I can use a querystring, but dont want it cause it can be modified by the user. and dont want to use session, unless its the only way.

     

    Thanks

     

    Wednesday, March 2, 2011 5:16 PM

Answers

  • User1704407637 posted

    Hi,

    You could check the Request.UrlReferrer in Page1 too se if you got there from Page2.

    Best Regards
    Johan 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 2, 2011 5:30 PM
  • User-610330605 posted

    I will have 8 pages, is this the best approach? I need Request.UrlReferrer in page2 becuase if going back from page3?

    Sorry, I should have mentioned I will have many pages....

    Suppose you have Page1.aspx, Page2.aspx, Page3.aspx & Page4.aspx.

    In Page1.aspx.cs you write

     

    uri urlReferrer = Request.UrlReferrer; 
    string previousPage = string.Empty; 
    if (urlReferrer != null) { previousPage = urlReferrer.AbsolutePath; } 
     
    if (!IsPostBack) 
    { 
        if (previousPage != "Page2.aspx")  
        { 
                    //load the page  
        } 
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 3, 2011 10:38 AM

All replies

  • User1704407637 posted

    Hi,

    You could check the Request.UrlReferrer in Page1 too se if you got there from Page2.

    Best Regards
    Johan 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 2, 2011 5:30 PM
  • User-610330605 posted

    I have page1.aspx contains (Next button), it redirects to Page2.aspx. Page2.aspx has a back button that redirects to page1.aspx

    I want to capture the action if page2.aspx (back button was clicked) and it made it redirect to page1.aspx page.

    Reason is because if the back button is clicked at   if (!IsPostBack) {...} I will reload the data that was entered when going from page1 to page2.

    You don't need to load data in Page1 when you go back from Page2? Are you sure about this?

    If yes then you can use Request.UrlReferrer as mentioned by coredev.

    Wednesday, March 2, 2011 6:15 PM
  • User-309740890 posted

    The Request.UrlReferrer is kinda working, but not exactly, here is a senario where it fails.

    Page1.aspx & Page2.aspx contains this: Need to check if the back button was clicked  then reload the data into controls

     if (!IsPostBack)
            {
                if (Request.UrlReferrer != nulll) <-----*

                    //load the page into controls
           }

     

    page1.aspx -> fill the data (saves into the session) -> next and it fails cause it goes inside the Request.UrlReferrer and is trying to load the data but the data is null.

    if I remove the if condition in page2.aspx, it works perfectly, when clicking the back button in page2, at page1.aspx it goes inside the if condtion and loads the data into controls.,

     

    maybe, is it possible to get the button id? so I would only load the data if button is back was clicked

    I hope it makes sense what I am trying to do, thanks

     

     

     

    Wednesday, March 2, 2011 6:58 PM
  • User-610330605 posted

    As I understand your requirement you don't nee to check Request.UrlReferrer in Page2.

    In Page1 instead of checking if Request.UrlReferrer is null, check if Request.UrlReferrer is the Url of Page2. Debug and see what exavtly is the value in Request.UrlReferrer in Page1 when you come from Page2.

    Thursday, March 3, 2011 1:53 AM
  • User-309740890 posted

    I will have 8 pages, is this the best approach? I need Request.UrlReferrer in page2 becuase if going back from page3?

    Sorry, I should have mentioned I will have many pages....

    Thursday, March 3, 2011 10:21 AM
  • User-610330605 posted

    I will have 8 pages, is this the best approach? I need Request.UrlReferrer in page2 becuase if going back from page3?

    Sorry, I should have mentioned I will have many pages....

    Suppose you have Page1.aspx, Page2.aspx, Page3.aspx & Page4.aspx.

    In Page1.aspx.cs you write

     

    uri urlReferrer = Request.UrlReferrer; 
    string previousPage = string.Empty; 
    if (urlReferrer != null) { previousPage = urlReferrer.AbsolutePath; } 
     
    if (!IsPostBack) 
    { 
        if (previousPage != "Page2.aspx")  
        { 
                    //load the page  
        } 
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 3, 2011 10:38 AM
  • User-309740890 posted

    HI there, it is working fine in Firefox but not in IE.. any ideas why? and how to fix that?

     

    please advice... thank you

    Thursday, March 10, 2011 12:37 PM
  • User1704407637 posted

    Hi,

    There can be issues regarding page names being rendered differently for example and there is also the case that the Request.UrlReferrer.AbsolutPath can contain a / so you could try to change the code to something like and see if that works

    Uri urlReferrer = Request.UrlReferrer;
                string previousPage = string.Empty;
                if (urlReferrer != null) { previousPage = urlReferrer.AbsolutePath.Replace("/", string.Empty).Trim(); }
    
                if (!IsPostBack)
                {
                    if (previousPage.ToLower() != "page2.aspx")
                    {
                        //load the page   
                    }
                }

    Best regards
    Johan

    Thursday, March 10, 2011 2:34 PM
  • User-610330605 posted

    HI there, it is working fine in Firefox but not in IE.. any ideas why? and how to fix that?

    Paste the URL.

    Thursday, March 10, 2011 2:50 PM
  • User-309740890 posted

    The value is empty only in IE, firefox is fine.

    when doing this:

     if (Request.UrlReferrer != null)
         Response.Write(Request.UrlReferrer.AbsolutePath);
     else
          Response.Write("Empty");

     

    clicking back from page2 to page1 in IE it is empty... any ideas why? thank you

    Thursday, March 10, 2011 6:12 PM
  • User-610330605 posted
    Do you use Response.Redirect or Server.Transfer in the Back button click in the second page?
    Thursday, March 10, 2011 6:24 PM
  • User-309740890 posted

    Response.Redirect

    Thursday, March 10, 2011 6:27 PM