Question

I need to do something that can only be processed on server side and not by just using javascript on the html page. I'm already using this process for my other aspx pages. However, this time I need to make this work on an html page.

The workaround that I have done so far is to have an iframe somewhere in my html page that points to an aspx page. I do the process on its code behind and save what I need on a hidden field in the iframe. I can now get that value from the iframe and use it on my html page.

It works but I'm thinking that there might be a simpler approach in doing this without having to create a new aspx page just for processing. Is there another way to accomplish this?

UPDATE: To be specific, I need to get the location(country) of the user on page load in order to change the css accordingly. I know there are a lot of sites that I can connect to on my page to do this but I don't know which one is secured. Also, we already have our own method to this for our aspx pages so I want to use that instead. Hope this update helps.

Was it helpful?

Solution

Here is how I would do what I think you are explaining. Your question was not very clear.

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

    $.ajax({
      type: "POST",
      url: "PageName.aspx/MethodName",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Do something interesting here.
      }
    });

HTML

    <html>
     <head>
      <title>Calling a page method with jQuery</title>
      <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
      <script type="text/javascript" src="Default.js"></script>
    </head>
    <body>
      <div id="Result">Click here for the time.</div>
    </body>
    </html>

JS

    $(document).ready(function() {
      // Add the page method call as an onclick handler for the div.
      $("#Result").click(function() {
        $.ajax({
          type: "POST",
          url: "Default.aspx/GetDate",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            // Replace the div's content with the page method's return.
            $("#Result").text(msg.d);
          }
        });
      });
    });

But again I really have no idea what you are trying to do. You were really vague.

OTHER TIPS

Yes, simpler approach is ajax.

This is pseudo javascript code...

window.onload = function(){
    ajax.open("your server url");
    ajax.on("success",function(){
        alert("okay, done.");
    });
};

Using iFrame for the process you have requested, is not recommended.

Consider including AJAX to route your request to a server resource. In your case it should be an asp page.

Here's a beginners guide to using ajax with asp http://www.w3schools.com/asp/asp_ajax_asp.asp

PS: When using AJAX make sure you are aware about

Here's a pseudo code for you to work on:

<script>
function funcName(){
var xmlhttp;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
 document.getElementById("<yourOwnElement'sID>").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","serverResource.asp?q="+<parameters>,true);
xmlhttp.send();
}
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top