• ASP.NET 4.5新特性一:强类型数据绑定(StronglyType DataBindings)


      随着ASP.NET 4.5的发布提供了很多的新特性,其中强类型数据绑定(Strongly-Type Data-Bindings)为我们的开发非常便利,且使用率非常之高,本篇博文和大家一起学习分享这一新特性,欢迎大家拍砖。

      首先回顾一下老式的数据绑定方式,比如需要将数据绑定到Repeater控件,通常会采用如下的实现方式。

    <ul>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <li>
                    <%Eval("FirstName"%>
                    <%Eval("LastName"%>
                </li>
            </ItemTemplate>
        </asp:Repeater>
    </ul>

      

      后台数据绑定方式不变,沿用DataSource提供数据源和DataBind()。

    protected void Page_Load(object sender, EventArgs e)
    {
        using (var db = new WebFormsLab.Model.ProductsContext())
        {
            this.customersRepeater.DataSource = db.Customers.ToList();
            this.customersRepeater.DataBind();
        }
    }

      在ASP.NET 4.5中提供了强类型数据绑定新特性,通过控件的ModelType指定要绑定的强类型对象全限定名,提供了新的数据绑定表示式<%#: Item.属性 %>,如上的Repeater控件的数据绑定采用新特性的实现如下代码块。

    <ul>
        <asp:Repeater ID="customersRepeater" ModelType="WebFormsLab.Model.Customer" runat="server">
            <ItemTemplate>
                <li>
                    <%#: Item.FirstName %>
                    <%#: Item.LastName %>
                </li>
            </ItemTemplate>
        </asp:Repeater>
    </ul>

      详细请访问:http://www.asp.net/vNext   

  • 相关阅读:
    设计模式 享元模式(池化技术)
    设计模式 混合模式(整体部分模式)
    设计模式 适配器模式
    Flex3示例、 安装 、注册码
    VS2010错误
    转载:glut.h 与 stdlib.h中 的exit()重定义问题的解决
    宿迁软件QQ群(109233721)
    百度地图 开发API接口啦
    Sublime Text 插件个人使用总结&推荐
    sublime text2 使用安装插件中文乱码问题解决
  • 原文地址:https://www.cnblogs.com/beniao/p/2385114.html
Copyright © 2020-2023  润新知