Solved

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

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

636 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