I am trying to create a simple ajax grid that allows me to add and remove rows and also edit some of the columns and have others columns calculated directly based on the input in the others. I thought this would be a nice oppurtunity to play with asp.net ajax 4.0 and the client templates. It works pretty ok but I can't seem to find a way to bind my json data to a . How can I do this?
A normal template looks like this
<div id="authorsTemplate" style="visibility:hidden;display:none;">
<ul>
<li>First Name: {{ FirstName }}</li>
<li>Last Name: {{LastName}}</li>
<li>Url: <a href="{{Url}}">{{Url}}</a></li>
</ul>
</div>
How would I use the data bind syntax with a dropdown
<select id="">
<option value="1">1</option>
<option value="2">2</option>
</select>
EDIT: If the select tag had a value attribute the obvious solution would be. Edit2: The syntax below was actually the solution, Thx Roatin, I had no idea the select actually had a value attribute.
<select id="" value="{binding nr}">
<option value="1">1</option>
<option value="2">2</option>
</select>
I could maybe use custom javascript to set the selected option but the point is a live binding to my json object the same way you bind to the value of a input tag
I have not actually tried this but it looks like any of these three links blog post could help. All the examples show binding to lists of data. Maybe something like this:
* Sample not tested my VS 2010 comp is busted and is closely derived from the third link *
Then to select the value you want (done the brute force way)
Or tweak the template to include a binding for a 'selected' option. ( Again not tested, you might not be able to perform Selected === true)
In fact,
select
DOM elements do have avalue
property (but not exposed as an attribute in the markup). Setting it is equivalent to (and faster than) iterating the child<option>
s and setting theselectedIndex
to the found option index of the option that has a matchingvalue
.Anyway, here's how to bind to it directly with
Sys.Binding
(complete test case):Here is how to do the same thing declaratively, if you prefer:
(and of course get rid of the
pageLoad
JavaScript stuff...)Both examples set up two-way binding to the
dataItem
object. You can see when the page loads, the third<option>
is selected as that is the option with a value matchingdataItem.Foo
. When you select a different item from the drop-down,dataItem.Foo
updates with the new value selected.Hope that helps!