1 Replies - 480 Views - Last Post: 06 December 2012 - 04:07 AM

#1 nmg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-April 12

calling an asp.net function with jquery or ajax

Posted 06 December 2012 - 02:36 AM

I am creationg an admin page, and I need to redirect the page after a few seconds if the user is not allowed to be there. there is supposed to be a session created when the admin has loged in, and i want to use jquery to check if the session exists.

here is the c# code:

protected void Page_Load(object sender, EventArgs e)
{
    if ((string)Session["admin"] == null)
    {
        mainF.InnerHtml = "<div id='inner' style='margin-left:370px; margin-top:80px;font: 0.92em arial,sans-serif;word-spacing: 2px;font-weight: bold;'>Your Are Not Supposed To Be Here.</div>";
    }
}
public bool checkIfAllowed()
{
    bool isIt = false;
    if ((string)Session["admin"] != null && (string)Session["admin"] != "")
    {
        isIt = true;
    }
    return isIt;
}

so basicly I thought that I can somehow use jquery to call the checkIfAllowed() function to check whether the session exists or not, but i dont know how.

this is what i have:
if (!checkIfAllowed() /*which of course doesn't work*/) {
        var inter = setInterval(function () {
            window.location.replace("http://www.google.com");
        }, 3000);
    }
    else {
        clearInterval(inter);
    }

there probably are other ways to do this, if you have a some solution please tell me. i guess i can also redirect the page with asp.net but it didn't work the first time. i also tried writing
$("#inner").html != null && $("#inner").html != ""

in the if() statement in the jquery, because #inner html is changing on pageLoad in asp.net.

thanks =]


Ps. i didn't know exacly what forum to write it in, maybe it better fits the asp.net one. If so, please move this topic.

Is This A Good Question/Topic? 0
  • +

Replies To: calling an asp.net function with jquery or ajax

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: calling an asp.net function with jquery or ajax

Posted 06 December 2012 - 04:07 AM

View Postnmg, on 06 December 2012 - 10:36 AM, said:

so basicly I thought that I can somehow use jquery to call the checkIfAllowed() function to check whether the session exists or not, but i dont know how.

that’s because there is no way to do exactly that. there is also no way for Java​Script (or jQuery or any other client-side language) to call any server-side function, because it (JS) is executed on the client.

what you can do is call a server-side script file that executes this function. (whether directly or via AJAX doesn’t matter).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1