zoukankan      html  css  js  c++  java
  • Asp.net控件开发学习笔记(十二)数据绑定

    Asp.net控件开发学习笔记(十二)----数据绑定

    数据绑定和服务器控件模板互为补充,数据绑定用于从数据库中提取数据,而上一章说的服务器控件模板是将这些数据以一定的格式显示在网页中,对于大多数网站来说,根据内容生成特定的html非常常见,而对于开发人员来说,从数据库中提取出数据再以一定的格式显示到网页上是经常需要完成的工作,而这些大多都利用GridView,Repeater等控件的数据绑定功能。

       一个典型的过程如下图:

      

    控件的数据绑定

       数据绑定有几种不同的形式,对于简单的数据绑定来说,仅仅是将一个数据表达式求值并将该值赋给某个属性,比如下面代码:

       <asp:Label id="MyLabel" runat="server" Text="<%= MyDataBoundMethod() %> />

    这段代码是将一个函数的返回值最为label的Text属性.

    而对于另外一些较为复杂的数据绑定控件来说,数据绑定意味着一大堆的数据以迭代的形式来帮定到一大堆的控件上来生成html.比如GridView会以Table的形式展现数据。而每一行<tr>内部用于格式的HTML会相同,而内容则有差异.

    数据绑定控件的基类

       和前面说的一样,在控件开发时选择合适的类作为基类是很重要的,而支持数据绑定的基类主要是从下面几个基类中去进行选择:

    System.Web.UI.WebControls.DataBoundControl:这个类是所有数据绑定类的基类。

    System.Web.UI.WebControls.CompositeDataBoundControl:这个类继承于DataBoundControl基类,用于复合服务器绑定控件

    System.Web.UI.WebControls.HierarchicalDataBoundControl:基于树的分层控件的基类

     

    在asp.net中,默认的数据绑定控件分布如下图:

     

    而用更清晰一些的视图来表示几个数据绑定基类之间的关系是:

     

    上面几个基类中,重写了DataBind方法(最开始是定义在Control当中),使DataBind调用PerformSelect,该方法能够检索有效的数据集合以使绑定发生。该方法是受保护的,因为它包含实现细节;它是抽象的,因为它的行为只能由派生类实现。

    如果不是基于IDataSource(比如那些SqlDataSource,也就是通过DataSourceID属性来定义的)来获取数据,PerformSelect方法内部会执行GetData方法来获取数据,而在调用GetData方法之前,会触发OnDataBinding事件。而之后则会触发DataBound事件

    下面是MSDN中PerformSelect的源码:

    protected override void PerformSelect() {           

     

       // Call OnDataBinding here if bound to a data source using the

       // DataSource property (instead of a DataSourceID), because the

       // databinding statement is evaluated before the call to GetData.      

        if (! IsBoundUsingDataSourceID) {

            OnDataBinding(EventArgs.Empty);

        }           

     

        // The GetData method retrieves the DataSourceView object from 

        // the IDataSource associated with the data-bound control.           

        GetData().Select(CreateDataSourceSelectArguments(),

            OnDataSourceViewSelectCallback);

     

        // The PerformDataBinding method has completed.

        RequiresDataBinding = false;

        MarkAsDataBound();

     

        // Raise the DataBound event.

        OnDataBound(EventArgs.Empty);

    }

    从代码中可以看出GetData利用OnDataSourceViewSelectCallback回调来调用PerformDatabinding方法,而PerformDataBinding方法又利用CreateControlHierarchy方法来构建子控件。在调用过CreateControlHierarchy方法后,这个方法会将空间内部的ChildControlIsCreated属性设置成True,从而数据绑定控件不会继续调用CreateChildControl来防止子控件被重复创建,如下是OnDataSourceViewSelectCallback和PerFormDataBinding的源码:

    private void OnDataSourceViewSelectCallback(IEnumerable retrievedData)

            {

                // Call OnDataBinding only if it has not already been

                // called in the PerformSelect method.

                if (IsBoundUsingDataSourceID)

                {

                    OnDataBinding(EventArgs.Empty);

                }

                // The PerformDataBinding method binds the data in the

                // retrievedData collection to elements of the data-bound control.

                PerformDataBinding(retrievedData);

            }}

    protected override void PerformDataBinding(IEnumerable data)

            {

                base.PerformDataBinding(data);

                Controls.Clear();

                ClearChildViewState();

                TrackViewState();

                if (IsBoundUsingDataSourceID)

                    CreateControlHierarchy(true, true, data);

                else

                    CreateControlHierarchy(true, false, data);

                ChildControlsCreated = true;

            }

     CreateChildControls方法一般用于创建组合控件。在此方法中可以定义自己需要的控件,进行实例化和赋值等,并将其添加到当前Controls集合中。数据绑定的生成方式和用此方法的方式的不同之处可用下图表示:

    1-5是数据绑定方式,而6-9是利用CreateChildControls方法来进行创建

  • 相关阅读:
    查询中常用的扩展方法
    加载关联表的数据 显式加载
    加载关联表的数据 延迟加载
    加载关联表的数据 贪婪加载
    操作内存中的数据
    DBContext基础查询
    EF简单增删改查
    1- MySQL数据库基础快速入门
    1-3 Postman 注册账号与登录
    1-2 postman工具简介
  • 原文地址:https://www.cnblogs.com/CareySon/p/1591807.html
Copyright © 2011-2022 走看看