Solved

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

Posted on 2013-01-21
3
278 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

792 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