Frage

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).

War es hilfreich?

Lösung

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();
};

Andere Tipps

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;
}

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top