ASP.NET 4.5新特性一:強型別資料繫結(Strongly-Type Data-Bindings)

來源:互聯網
上載者:User

  隨著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   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.