Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass Server Side object to client side jscript

Posted on 2006-11-27
4
Medium Priority
?
292 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 2000 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

Hire Technology Freelancers with Gigs

Work with 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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

927 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