SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calling asp.net function from javascript

    I have a javascript function that will execute each time a user tried to leave the page that they are currently viewing. When this happens, I would like a asp.net function to be called (CheckIfSaved()) which retuns true/false.
    How can a go about calling this function from the javascript function?

    The javascipt code is kept in a seperate file (javascript.js)
    The CheckIfSaved() is kept in default.aspx.cs

    At present, i can get my javascript function to work if I add:

    Code:
    function CheckIfSaved()
    {
    	return false; // your code does this
    }
    into the javascipt.js file.

    Therefore i take it that the javascipt does not know where to access the CheckIfSaved() asp.net function. (when using the javascipt console in firefix, I got the following error: "CheckIfSaved is not defined"

    How can I tell javascript where to find this function?

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    have your asp page check for the GET of logout=true and if it exists have it execute it...

    then in the javascript do something like this:

    document.onunload = function(){
    document.write('<script type="text/javascript" src="locationofyourasppage.asp?logout=true"></script>';
    }

    as for trying to do what you want is impossable... you cannot put asp code in javascript and expect it to run it the same... besides the browser will need to contact the server to get a true logout...

    this code should do what you want, but it is untested... so i am not sure if it will actually access the page in time before it closes or even execute that code... you might beable to add "for(i=0;i<10000;i++);" after the "document.write" line (you will need to adjust that 10000 number thought). but that might not work either, but worth a shot.

    or you could have it do a popup, but since modern day browsers all have popup blockers it will not do much and probably get blocked.

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im using the following code:

    Code:
    function loadUrl(url)
    {
    	if(!CheckIfSaved())
    	{
    		var load = confirm('Do you want to leave without saving?');
    		if(load)
    		{
    			location.href = url;
    		}
    	}
    }
    
    function getLinks(element)
    {
    	var links;
    	if(!element)
    		links = document.getElementsByTagName('A');
    	else
    		links = element.getElementsByTagName('A');
    
    	for(var i = 0; i < links.length; i++)
    	{
    		if(links[i].getAttribute('title').indexOf('External Link') > -1)
    		{
    			links[i].href = 'javascript:loadUrl(\'' + links[i].href + '\');';
    		}
    	}
    }
    
    window.onload = function()
    {
    	getLinks(null);
    }
    and this code is called when one of the following links are clicked:

    Code:
    <div id="tabs10" style="width: 1000%; height: 43px">
        <ul>
            <li><a href="ClientHomePage.aspx" title="External Link - "><span style="font-size: 9pt; font-family: Arial">Home</span></a>
            <li><a href="BasicDetails.aspx" title="External Link - "><span style="font-size: 9pt; font-family: Arial">Basic</span></a> </li>
    </ul>
    </div>
    is there any way of me calling the CheckIfSaved() function on the click event on the above links, then pass the return value (true/false) to the javascript function? If so, any hints on the javascipt code to pass this variable accross?

  4. #4
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, that is what i am saying... you need to have javascript call the html page as a javascript or as an iframe...

    I do not know asp, but i know vb which are similer... so let me see your code for that function and maybe i can convert it into javascript for you.

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  5. #5
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
            protected bool CheckIfSaved()
            {
                bool Saved = true;
    
                BasicContactDetails basicdetails = new BasicContactDetails();
                basicdetails = BasicContactDataFunctions.GetBasicContactDetails(userID);
    
                if (basicdetails == null)
                {
                    if (Surname.Text != "") { Saved = false; }
                    if (Forename.Text != "") { Saved = false; }
                    if (MiddleName.Text != "") { Saved = false; }
                    if (MaidenName.Text != "") { Saved = false; }
                    if (PhoneNumber.Text != "") { Saved = false; }
                    if (MobileNumber.Text != "") { Saved = false; }
                    if (PANumber.Text != "") { Saved = false; }
                    if (Email.Text != "") { Saved = false; }
                    if (Fax.Text != "") { Saved = false; }
                    if (OfficePhone.Text != "") { Saved = false; }
                    if (Notes.Text != "") { Saved = false; }
                }
                else
                {
                    if (Surname.Text != basicdetails.Surname) { Saved = false; }
                    if (Forename.Text != basicdetails.ForeName) { Saved = false; }
                    if (MiddleName.Text != basicdetails.MiddleName) { Saved = false; }
                    if (MaidenName.Text != basicdetails.MaidenName) { Saved = false; }
                    if (PhoneNumber.Text != basicdetails.HomePhone) { Saved = false; }
                    if (MobileNumber.Text != basicdetails.Mobile) { Saved = false; }
                    if (PANumber.Text != basicdetails.PANumber) { Saved = false; }
                    if (Email.Text != basicdetails.Email) { Saved = false; }
                    if (Fax.Text != basicdetails.Fax) { Saved = false; }
                    if (OfficePhone.Text != basicdetails.OfficePhone) { Saved = false; }
                    if (Notes.Text != basicdetails.Notes) { Saved = false; }
                }
    }

  6. #6
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ya you will need to do something like i said origionally you will need to do something like:

    HTML Code:
    <script type="text/javascript">
    function checkserver(page){
      document.write('<script type="text/javascript" src="aspcheckpage.asp?checksave=true&page='+page+'"></script>');
    return false;
    }
    </script>
    then call it with DHTML as like: onclick="checkserver(this.href)"

    then make an asp page that will make output the header as "content-type: text/javascript" and have it do your check... then have it output:

    check = confirm('did you want to save?');
    if(check){
    //save it
    window.location='(HERE YOU WILL NEED TO RETURN the GET VALUE OF page)";
    }else{
    //dont save it
    window.location='(HERE YOU WILL NEED TO RETURN the GET VALUE OF page)";
    }

    only if it is not saved. if it does not need to be saved, have it return:

    window.location='(HERE YOU WILL NEED TO RETURN the GET VALUE OF page)";

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •