Solved

c# javascript how do I add an array to javascript from c# that I can search on?

Posted on 2013-01-21
3
258 Views
Last Modified: 2013-01-24
I have a c# page with javascript.  I need to create a javascript function that will look up properties based on an id.  I want to write all of the properties belonging to the id to the page so that I can search it via javascript.  I am currently converting from using a webservice
to do this, which was too slow.

I need a function like
GetProperty(id, 'name')
GetProperty(id, 'description')

How do I write the values to the page and put them in a variable?  How do I search this object for my values?

the info for the page
id, name, desc
1, 'myname', 'mysdescription'
2, 'myname2', 'mysdescription2'
3, 'myname3', 'mysdescription3'
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 10

Expert Comment

by:Monica P
ID: 38804316
Is it ok to use JSON method to get info from server side to client side through JAVAscript?
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 38805140
Depending on the number of items in your array, you should consider whether to include them in your page...
If it's a simple object in C# like a generic list, the string representation (JSON) of the object is determined as follows :
new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(yourList);

Open in new window

So in order to have it available clientside as an array you can use
Page.ClientScript.RegisterArrayDeclaration("yourArrayName", System.Web.Script.Serialization.JavaScriptSerializer().Serialize(yourList));

Open in new window

In order to get the proper value from the array, you have to iterate through your array.
// usage : var prop = GetProperty(1, 'description')
function GetProperty(id, property) {
   for(var x = 0, arrLen = yourArrayName.length; x < arrLen; x++) {
      if(yourArrayName[x].id === id)
         return yourArrayName[x][property];
   }

   return null; // array item not found; return null
}

Open in new window

0
 
LVL 25

Expert Comment

by:apeter
ID: 38807592
Did you try JQuery ?
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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

919 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

13 Experts available now in Live!

Get 1:1 Help Now