4

I have two pages 1. a.aspx and 2. b.aspx I pass query string from "b.aspx?save=success" to a.aspx. In Page Load of a.aspx I have the following code:

Page_Load()
{ 
    if(!Postback)
    {
        if (Request.QueryString["save"] != null) 
        {
            noDataFound.InnerHtml = "operation success";
        }
    }
}

Problem: On load of a.aspx page I get the message "operation success". This is Ok.But When I refresh the page again I get the same message as "operation success". How not to display again the same message on page refresh(pressing F5or reload).

Yahya
  • 3,386
  • 3
  • 22
  • 40
Vivek Ranjan
  • 1,432
  • 2
  • 15
  • 37

3 Answers3

3
function invokeMeMaster() {

    var isPostBack = <%= Page.IsPostBack ? "true" : "false" %> ;

    if (!isPostBack) {

        /* START */
        var query = getQueryParams(document.location.search);

        var p = query.save;
        if (sessionStorage.hits) {
            sessionStorage.hits = Number(sessionStorage.hits) + 1;
        } else {
            sessionStorage.hits = 1;
        }

        if (p == "success" && (sessionStorage.hits) % 2 == 0) {

            document.getElementById("<%=noDataFound.ClientID %>").innerText = "Testing...........";
        }

        function getQueryParams(qs) {
            qs = qs.split("+").join(" ");

            var params = {}, tokens,
            re = /[?&]?([^=]+)=([^&]*)/g;

            while (tokens = re.exec(qs)) {
                params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
            }

            return params;
        }

        /* END */

    } else {
        document.getElementById("<%=noDataFound.ClientID %>").innerText = "";

    }
}
window.onload = function () {
    invokeMeMaster();
};
Ganesh Jadhav
  • 2,830
  • 1
  • 20
  • 32
Vivek Ranjan
  • 1,432
  • 2
  • 15
  • 37
  • Done through client side. Using session was not recommended in .cs page. Though I am using SessionStorage here, it also doesn't solve my problem completely because it just compare the count. – Vivek Ranjan May 20 '14 at 14:13
2

untested solution (Keeping F5 or Reload of Page in mind), may be you have do something like below:

if(!IsPostBack)
{ 

  if (Request.QueryString["save"] != null && Session["CheckSuccess"] == null)
  { 
    noDataFound.InnerHtml = "operation success";
    Session["CheckSuccess"] = "true";
  } 
  else
    noDataFound.InnerHtml = string.Empty;
}
Hassan
  • 5,360
  • 2
  • 22
  • 35
  • Is there any way except using Session, because it makes the page heavy. – Vivek Ranjan May 02 '14 at 10:47
  • I think to maintain the state `Session` is good. It won't create any difference in page loading. – Hassan May 02 '14 at 10:51
  • Or can we store Query string in some collection? – Vivek Ranjan May 02 '14 at 10:54
  • ofcourse you can maintain `NameValueCollection`. But you will have same key/value every time unless you call your page with different query strings. – Hassan May 02 '14 at 10:57
  • Any Sample you can share? – Vivek Ranjan May 02 '14 at 11:00
  • [See here](http://www.dotnetperls.com/querystring). Link tells how you can get `NameValueCollection` from querystring. – Hassan May 02 '14 at 11:03
  • 1
    F5 will just send the same data as the first time, figuring out that the request is already handled should be on the server. Using the session is a good solution if you want the page to be empty. However showing "operation success" again is just how the web works, adding session stuff will make your pages more complex and probably will introduce nasty bugs in the future. BTW, putting stuff in the query string will make your webapplication vulnerable for xss and injection. – Peter May 02 '14 at 11:04
  • @peer explained it very well. +1 – Hassan May 02 '14 at 11:05
1

The best I can think of is using the IsPostback property to check that.

if (!this.IsPostback)
{
    // first try
   if (Request.QueryString["save"] != null)
   {noDataFound.InnerHtml = "operation success";}
}

NOTE: IsPostback is not set on refresh, only if clicking a button or something alike triggers the ASP.NET postback action.

The other thing you could do is set a Session variable then the 'operation succesful' must be shown (probably you determine this in another Page.

// other page
Session["showSaveMessage"] = true;

// this page
if (Session["showSaveMessage"] == true)
{
    // show message
    Session["showSaveMessage"] = false;
}

A third option is to move this client side. Create a javascript action on load of the page. When a specific part is added to the query string (#showmessage), you can catch that and show the message (How to get the value from the GET parameters?).

Then redirect to the parameterless version (#) by setting the url to the stripped version. Set window.location.href or window.location.search for that (this won't cause a call to the webserver, since it is all client side).

This circumvents the drawbacks of the first solution, but introduces more code client side. Luckily, ASP.NET MVC has some mechanisms for this. Unfortunately ASP.NET Web Forms doesn't have those.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325