How to reload the same page on a button click , with the data from database and ignoring the changes

This line of code is read the url as it is, and redirect you to the same page with out new post. Place it on button click, on code behind.

Response.Redirect(Request.RawUrl);

P.S. If you just make post, the input controls will keep their data, so you need to reload the page from the beginning, with the redirect. If you not make redirect you need to handle the controls and clear them.

Additional you can avoid the post back and round trip to the server and do the same on client side with javascript calling the OnClientClick="return ReloadPage();" with javascript code:

function  ReloadPage()
{
    top.location.replace( updateURLParameter(document.location.href, "_rnd", Math.random()) );
    return false;
}
//
// this function is an improved version of
// http://stackoverflow.com/a/10997390/159270
//
function updateURLParameter(url, param, paramVal)
{
    var TheAncor = null;
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var additionalURL = tempArray[1];
    var temp = "";

    if (additionalURL) 
    {
        var tmpAncor = additionalURL.split("#");
        var TheParams = tmpAncor[0];
            TheAncor = tmpAncor[1];
        if(TheAncor)
            additionalURL = TheParams;

        tempArray = additionalURL.split("&");

        for (i=0; i<tempArray.length; i++)
        {
            if(tempArray[i].split('=')[0] != param)
            {
                newAdditionalURL += temp + tempArray[i];
                temp = "&";
            }
        }        
    }
    else
    {
        var tmpAncor = baseURL.split("#");
        var TheParams = tmpAncor[0];
            TheAncor  = tmpAncor[1];

        if(TheParams)
            baseURL = TheParams;
    }

    if(TheAncor)
        paramVal += "#" + TheAncor;

    var rows_txt = temp + "" + param + "=" + paramVal;
    return baseURL + "?" + newAdditionalURL + rows_txt;
}

The above code is adding a random number to the end of the url, or update it if exist to avoid to keep the page on cache. If you all ready avoid to keep the page on cache you can do the same with out the extra random parameter.

Tags:

Asp.Net