Solved

Pass Server Side object to client side jscript

Posted on 2006-11-27
4
247 Views
Last Modified: 2012-05-05
Okay, I have spent the entire day trying to figure how to make this work.  I am trying to pass an object to a javascript function which will pass it to another window.  The script works perfectly when I send a control (like a textbox or textarea), but I need to pass it a class.  I created a test class with three public properties (for testing), it has properties for lastname, firstname and address.  On the server side I have mapped values to it like so.

Dim test As New test

With test
    .LastName = txtlast.Text
    .FirstName = txtfirst.Text
    .Address = txtaddress.Text
End With

In my client side script what I currently have is

var oText = document.getElementById("InfoArea").value;

Again, this works perfectly since "InfoArea" is a HTML textarea, but I need to pass the test object from the server side.  I am not sure this is even possible the way that I have approached it.  Please let me know if I can provide more information.

thank you for your input,
Clint...
0
Comment
Question by:clintnash
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18030262
Without AJAX I don't believe that what you are trying to do will work.

Bob
0
 
LVL 6

Accepted Solution

by:
manicsquirrel earned 500 total points
ID: 18043850
I don't know if you can pass the object from server-side, but if you can try this:

<script type="javascript"> //must be placed inside the body tags NOT the head
   
   var obj = new Object();
   obj = <%= getMyObject() %>;

  //to test the returns
   alert(obj.LastName);
   alert(obj.FirstName);
   alert(obj.Address);

</script>

Code Behind:

Public Function getMyObject() as Object
   With test
      .LastName = txtlast.Text
      .FirstName = txtfirst.Text
      .Address = txtaddress.Text
   End With

   return test

End Function


If that doesn't work I know you CAN pass the values:


<script type="javascript"> //must be placed inside the body tags NOT the head
   
   var firstName = <%= getLastName() %>;
   var lastName= <%= getFirstName() %>;
   var address= <%= getAddress() %>;

  //to test the returns
   alert(firstName );
   alert(lastName);
   alert(address);

</script>

Code Behind:

Public Function getFirstName() as string
   retun txtfirst.Text
End Function

Public Function getAddress() as string
    return txtaddress.Text
End Function

Public Function getLastName() as string
      return  txtlast.Text
End Function

Or you could just do this:

<script type="javascript"> //must be placed inside the body tags NOT the head
   
   var firstName = <%= txtfirst.ClientID() %>.value;
   var lastName= <%= txtlast.ClientID() %>.value;
   var address= <%= txtaddress.ClientID() %>.value;

  //to test the returns
   alert(firstName );
   alert(lastName);
   alert(address);

</script>

No code behind

There are probably dozens more ways to pass data from server side to client side.  However, you want to pass an object that contains the value of client side objects.  Thos textboxes may be asp controls, but they are already on the client side.  Using the ClientID property is the easiest way to access them, especially if they are nested in a NamingContainer like a formview.

hope this helps!  I haven't tested any of it.
0
 
LVL 6

Expert Comment

by:manicsquirrel
ID: 18043876
Oh, you can create the object collection on the client-side as well:

var obj = new Object();
obj.firstName = <%= txtfirst.ClientID() %>.value;
obj.lastName= <%= txtlast.ClientID() %>.value;
obj.address= <%= txtaddress.ClientID() %>.value;

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now