Solved

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

Posted on 2013-01-21
3
248 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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try JQuery ?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

762 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

7 Experts available now in Live!

Get 1:1 Help Now