asp.net AJAX 4.0的客户端模板,如何绑定一个选择? [英] asp.net ajax 4.0 client templates, how to bind a select?

查看:248
本文介绍了asp.net AJAX 4.0的客户端模板,如何绑定一个选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的Ajax格,让我根据其他输入添加和删除行,还可以编辑一些列的,具有直接计算其他列。我认为这将是一个不错的奥珀蒂尼蒂用asp.net AJAX 4.0和客户端模板玩。它的工作原理pretty确定,但我似乎无法找到我的JSON数据绑定到的方法。我怎样才能做到这一点?

一个正常的模板看起来像这样

 < D​​IV ID =authorsTemplate的风格=能见度:隐藏;显示:无;>
    < UL>
        <李>首先名称:{{名字}}< /李>
        <立GT;姓:{{姓氏}}< /李>
        <立GT;地址:LT; A HREF ={{网址}}> {{URL}}< / A>< /李>
    < / UL>
< / DIV>

我将如何使用数据绑定语法下拉

 <选择ID =>
  <期权价值=1> 1 LT; /选项>
  <期权价值=2> 2'; /选项>
< /选择>

编辑:
如果选择标记有一个属性值明显的解决办法是。
EDIT2:
下面的语法实际上是解决方案,THX Roatin,我不知道选择其实是有一个value属性。

 <选择一个id =值={结合NR}>
    <期权价值=1> 1 LT; /选项>
    <期权价值=2> 2'; /选项>
< /选择>

我也许可以使用自定义JavaScript来设置选择的选项,但该点活绑定到我的JSON对象绑定到一个输入变量的值

以同样的方式
解决方案

  

我不想来填充下拉列表中,它已经填充。我希望能够使用客户端模板绑定语法从我的JSON对象的值,并把所选元素。


其实,选择 DOM元素的的有属性(但不暴露在标记的属性)。设置它相当于(和速度比)迭代子<选项> 和设置的selectedIndex 来的具有匹配的选项选项发现指数

总之,这里是如何绑定到它直接与 Sys.Binding (完整的测试用例):

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// EN
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
< HEAD>
    <风格类型=文/ CSS>的.sys模板{显示:无}< /风格>
    &所述; SCRIPT SRC =MicrosoftAjax.debug.js>&下; /脚本>
    &所述; SCRIPT SRC =MicrosoftAjaxTemplates.debug.js>&下; /脚本>    <脚本类型=文/ JavaScript的>
    VAR的DataItem = {美孚:'3'};
    功能页面加载()
    {
        $创建(Sys.Binding,{
            目标:$获得(名单),
            targetProperty:'值',
            来源:DataItem的,
            路径:'富',
            模式:Sys.BindingMode.twoWay
        });
    }
    < / SCRIPT>
< /头>
<身体GT;
  <选择一个id =清单>
    <期权价值=1>的One< /选项>
    <期权价值=2>二< /选项>
    <期权价值=3>三< /选项>
  < /选择>
< /身体GT;
< / HTML>

下面是如何做同样的事情声明,如果preFER:

 <机身的xmlns:SYS =JavaScript的:SYS
      的xmlns:绑定=JavaScript的:Sys.Binding
      SYS:激活=*>  <选择ID =清单
    SYS:附加=绑定
    绑定:目标={{$得到('名单')}}
    绑定:targetproperty =值
    绑定:来源={{DataItem的}}
    绑定:路径=富>
    <期权价值=1>的One< /选项>
    <期权价值=2>二< /选项>
    <期权价值=3>三< /选项>
  < /选择>

(当然摆脱页面加载 JavaScript的东西...)

这两个例子都建立双向绑定到的DataItem 对象。你可以看到在页面加载时,第三<选项> 选择,因为这是一个值的匹配选项 dataItem.Foo 。当您从下拉不同的项目, dataItem.Foo 与选定的新值更新。

希望帮助!

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 do not want to populate the dropdown, it is already populated. I want to be able to use the client template binding syntax to get the value from my json object and set the selected element.

In fact, select DOM elements do have a value 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 the selectedIndex to the found option index of the option that has a matching value.

Anyway, here's how to bind to it directly with Sys.Binding (complete test case):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">.sys-template {display:none}</style>
    <script src="MicrosoftAjax.debug.js"></script>
    <script src="MicrosoftAjaxTemplates.debug.js"></script>

    <script type="text/javascript">
    var dataItem = { Foo: '3' };
    function pageLoad()
    {
        $create(Sys.Binding, {
            target: $get("list"),
            targetProperty: 'value',
            source: dataItem,
            path: 'Foo',
            mode: Sys.BindingMode.twoWay
        });
    }
    </script>
</head>
<body>
  <select id="list">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</body>
</html>

Here is how to do the same thing declaratively, if you prefer:

<body xmlns:sys="javascript:Sys"
      xmlns:binding="javascript:Sys.Binding"
      sys:activate="*">

  <select id="list"
    sys:attach="binding"
    binding:target="{{ $get('list') }}"
    binding:targetproperty="value"
    binding:source="{{ dataItem }}"
    binding:path="Foo">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>

(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 matching dataItem.Foo. When you select a different item from the drop-down, dataItem.Foo updates with the new value selected.

Hope that helps!

这篇关于asp.net AJAX 4.0的客户端模板,如何绑定一个选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆