Solved

Pass Server Side object to client side jscript

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust the position 3 49
What are the Important skill to have as Asp.net Developer 8 43
asp.net mvc return json 2 57
Displaying a web form as a modal popup dialog box. 4 24
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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

20 Experts available now in Live!

Get 1:1 Help Now