Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pass Server Side object to client side jscript

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert html to string 6 38
Initial get page response times and IIS8 15 55
Need to start a web service from Visual Studio 2015 Pro 2 35
Expression Evaluater 3 37
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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