views:

138

answers:

3

I am using ASP.NET 3.5.

In my code behind i have this code that i want to execute from my JavaScript.

Private Sub CreateName()

   Dim Name as String
   Name = txtName.text

End Sub

And this is my JavaScript Function

  <script type="text/javascript">

   function doSomething() { 
   document.elqFormName.action = 'http://now.eloqua.com/e/f2.aspx' 
   document.elqFormName.submit();

  }
 </script>

So what must I place inside my JavaScript function to execute my function in my code behind?

Thanks in advanced!!

A: 

I'm not sure how VB works, but it's similar to C#. I've previously done this by making a WebMethod and using ajax.

Jimmeh
Attached a link to MSDN.
Jimmeh
With regard to the javascript and ajax, i'd advise using jquery if you can.
Jimmeh
Thanks, your link shows me how the WebMethod worked but I still dont know how I would call it from my Javascript method.
Etienne
You'd need to use ajax, and i don't think you could change values on the page from it, since it would be a static method. It may not be of much use judging by your code behind sample. It's worth knowing for the future though, if you're working with ASP.Net, as server/client separation can often be a problem.
Jimmeh
Thanks, yeah i have noticed that server/client separation can be a problem......ai ai ai
Etienne
A: 

Although you could do this using WebMethods as Jimmeh stated, another option would be to use a HTML generic handler. In this approach, your CreateName method wouldn't be in a ASPX page but in a ASHX page.

Check:

http://www.aspcode.net/Creating-an-ASHX-handler-in-ASPNET.aspx

Inside your doSomething method in the javascript part you'd need to call the ASHX using Ajax.

Check:

http://docs.jquery.com/Ajax

Ciwee
Thanks, but i am not really sure what i mut be looking at on this link http://docs.jquery.com/Ajax
Etienne
It's an example of how you can call an ASHX file. You could use any other Ajax approach such as the default XMLHttpRequest.
Ciwee
A: 

I had this same issue, and I found the easiest way to handle it was with an AJAX call. In your ASPX page (javascript):

//======================================
/* This function creates a new instance of an XMLHttpRequest object, 
   based on the users browser, and returns it  */
//======================================      
var xmlhttp
function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
}

//======================================
/* This function issues a request and specifies which function should handle the ajax response  */
//======================================  
function doSomething()
{
    xmlhttp = GetXmlHttpObject();
    xmlhttp.onreadystatechange=stateChanged;

    var url = "CreateName.aspx"

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
}

//======================================
/* This function handles the ajax response text, places it in a label  */
//======================================  
function stateChanged()
{
  if (xmlhttp.readyState==4)
  {
      var returned = xmlhttp.responseText;
      document.getElementById("lbl_returnStatus").innerHTML = returned;
  }

}

And then in the file CreateName.aspx:

<%
'Here is where you can do anything on the server side
Dim Name as String         
Name = txtName.text

'This is what will be passed back and handled by the stateChanged function
Response.Write("Success!")
%>

You can also pass parameters through an AJAX call if you need to. Since the type of request we are making is a GET, you can just add the parameters to the URL in Javascript and access them server side with the Request.Querystring("paramName") function.

I wrote a more detailed post on starting AJAX on my blog, here, if you'd like to read that as well. Cheers!

David Hague