Solved

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

Posted on 2013-01-21
3
287 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

726 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