Hello All,
what is code for exceute one aspx page onclick hyperlink without dispaly that page?

Basically, I have one page p2.aspx.,which getting querystring parameter from page p1.aspx.
When I cliced on hyper link of p1.aspx page .it redirect to the p2.aspx.
but i want only that the code on p2.aspx excecute.and not need to dispaly p2.aspx page to user.

so what is the code on hyper link at p1.aspx in source file?

Thanking you,
Parimal.

Dani AI

Generated

Nice solution from — the core idea is to call the server page asynchronously so its code runs but the browser does not navigate. A few practical refinements and safer alternatives for someone coming to this thread years later:

Use a small AJAX/fetch request from the hyperlink click and keep server responses minimal (JSON or 204 No Content) so you do not inject a whole page into the UI. Example client pattern:

// call p2.aspx without redirecting the browser
fetch('p2.aspx', {
  method: 'POST',
  credentials: 'same-origin',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: 'name=jigsaw'
})
.then(resp => resp.status === 204 ? null : resp.text())
.then(result => { /* optional: show status or ignore */ });

On the server prefer returning a small status payload or 204 instead of full HTML:

protected void Page_Load(object sender, EventArgs e)
{
    // run whatever server work is needed
    DoBackgroundWork(Request.Form["name"] ?? Request.QueryString["name"]);
    Response.StatusCode = 204;
    Response.SuppressContent = true;
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

Best practices and troubleshooting:

  • If the action changes server state, use POST and require authentication and an anti-forgery token; never perform destructive work via GET.
  • Consider refactoring the work into a WebMethod, handler (.ashx) or API endpoint rather than a full ASPX page — cleaner and easier to test.
  • Use browser devtools Network tab to confirm the request, status code and response body; watch for CORS, caching or incorrect Content-Type issues.
  • If you want a library, jQuery.ajax or the modern fetch API both work; mentioned jQuery as an alternative and that is still a fine option.

This keeps the UI on p1.aspx while p2.aspx logic runs, and improves maintainability and security over returning full HTML.

Recommended Answers

All 3 Replies

ok i've made a Demo 4 you..

all you have to do is copy and paste..

This is the aspx code for my Default.aspx page..
As you can see in the javascript it calls the Default4.aspx page and
i've appended a data "Jigsaw" as a querystring..

in code behind there is not code..

<head runat="server">
    <title></title>

    <script src="PureAjax.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        function MyMethod() {
            ajaxCall("POST", null, "Default4.aspx?name=jigsaw", "aa");
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="ButtonX" runat="server" Text="Button" OnClientClick="MyMethod();return false;" />
    
    </div>
    <div id="aa">
    Testing Pure Ajax...
    </div>
    </form>
</body>
</html>

Let's move on to Default4.aspx page , on Design page add a Literal Control.
Done.

In code behind write the code...

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["name"] != null)
        {
            Literal1.Text = "Name which you passed in was : " + Request.QueryString["name"].ToString();
        }
    }

as you can see it grabs the querystring name from the previous page and assigns value to Literal..
Done..

Final Js Code..you can Make a .jS and refrence to the Default.aspx page

var req;
function ajaxCall(p_submitMethod, p_parameters, p_url, p_targetDiv) {
    document.getElementById(p_targetDiv).innerHTML = 'loading data...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() { ajaxResponse(p_targetDiv); };

        if (p_submitMethod == "POST") {
            req.open("POST", p_url, true);
            req.send(p_parameters);
        }
        else {
            req.open("GET", p_url, true);
            req.send(null);
        }

    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { ajaxResponse(p_targetDiv); };

            if (p_submitMethod == "POST") {
                req.open("POST", p_url, true);
                req.send(p_parameters);
            }
            else {
                req.open("GET", p_url, true);
                req.send();
            }


        }
    }
}


function ajaxResponse(p_targetDiv) {
    
    if (req.readyState == 4) {
    
        if (req.status == 200 || req.status == 304) {
            results = req.responseText;
            //alert(results); Use for Debugging...
            document.getElementById(p_targetDiv).innerHTML = results;

        } else {
            document.getElementById(p_targetDiv).innerHTML = "Some Error :" + req.statusText;
        }
    }
}

function createTimestamp() {
    return "timestamp=" + new Date().getTime().toString();
}

All Set...try this and let me know the output
hope that helps..!

Wow,Great ITs Really work very well and exactly that I waited...

Thank you So much ,Mr.Ashish.

wow seems like u did check my personal profile.. cool :)
but in these days it can be easily done via Jquery..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.