tags:

views:

69

answers:

3

Hi ,

I am doing an application where there will be multiple items displayed on a page. for each item there will be lot of info. The data is completly generated in the code behind .aspx.cs and displayed in the ui.

When the user mouseover particular item , apart from visible elements i need some other info from the code behind to grab for that item. Right now i am storing lot of info with pipe separated in a hidden divs.

When the user mouseovers a item , i go to the context of that div and get the hidden values and show some popups..I just want to know if this is the right approach or if there is any other nice way of storing all the info as a object on the client side.

I am using jquery on the UI side.

+1  A: 

You can store your data in a JavaScript object. Generate JSON on the back-end and parse it (or simply assign it as a literal to a variable) on the client side.

Ates Goral
I am not making any ajax call for the first popup. I have only one option to store data somewhere in the html and get through jquery. Do you know any way to store data from codebehind file like .cs to dom and then access the same in the context of each object. Each record on teh click side sould have that object
gov
You don't need AJAX. And you don't need jQuery. All you have to do is to populate an object with your data on the C# side and embed a `<script>` element in your page where you assign the JSON-serialized version of that object (using `JavaScriptSerializer`) in a global variable. The result would look something like: `<script type="text/javascript">var records = [ {}, {}, ... ];</script>` Then you can simply access the data in that global variable using JavaScript.
Ates Goral
Thank you very much.
gov
+1  A: 

Personally, I've often used html attributes for this purpose, as creating separate div for each value isn't exactly concise or readable. Something like this <div class="feed" feed_id="id goes here" some_other_data="some other value">

You can also employ new data attributes from HTML 5, thus making your page valid HTML (if you care about passing validation at all).

edit
Javascript may also be a solution, as Ates notes, but IMHO it only works for small amount of information. If you consider example above, giving 'feed_id' value to each element with class 'feed' isn't exactly trivial in JavaScript.

Nikita Rybak
THanks nikita for your explanation
gov
+2  A: 

jQuery allows you to store data against DOM objects in a much more elegant way.

See the data() api here: http://api.jquery.com/jQuery.data/

Another way that is used often but can make your X/HTML non compliant is creating your own attributes.

eg. <div id="myDiv" someattr="lipsum dolor sit emet"> ... </div>

and then in your jQuery you can get the value by doing $('myDiv').attr('someattr')

Moin Zaman
I know data() and used it in some contexts, but i am not making any ajax calls to the backend. On the mouseover of a item i need to grab the data from dom.
gov
data() doesn't have anything to do with making ajax calls to the backend. you can set it at any point, even page load, and then onmouseover use the values you've set to show them in a popup
Moin Zaman
Thanks moin for your explanation.
gov
finally i used jquery.metadata plugin
gov