Solved

Pass Server Side object to client side jscript

Posted on 2006-11-27
4
263 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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