ASP.NET/ObjectDataSource

維基教科書,自由的教學讀本

ObjectDataSource 控制項用作數據源。可以與各種界面控制項( GridView、FormView、DetailsView、DropDownList)做數據綁定。

TypeName 屬性標識 ObjectDataSource 使用的類的名稱。 ObjectDataSource 控制項在每次調用方法時都創建並銷毀類的實例。

由 SelectMethod、UpdateMethod、InsertMethod 和 DeleteMethod 屬性標識的方法可以是實例方法或 static(在 Visual Basic 中為 Shared)方法。 如果方法為 static(在 Visual Basic 中為 Shared),則不創建業務對象的實例,也不引發 ObjectCreating、ObjectCreated 和 ObjectDisposing 事件。如果方法簽名帶參數,可以將 Parameter 對象添加到ObjectDataSource對象的SelectParameters屬性集合對象中,然後將它們綁定到要傳遞給屬性指定的方法。 為使 ObjectDataSource 能夠使用參數,這些參數必須與方法簽名中的參數名稱(注意大小寫)和類型相匹配。DataSourceID 屬性自動綁定到數據源中的數據;設置DataSource 屬性,但之後必須顯式調用數據綁定控制項的 DataBind 方法。如果 ObjectDataSource 控制項與數據綁定控制項相關聯,則由數據綁定控制項自動添加參數,所以必須保證方法的參數名稱與數據綁定控制項中的欄位名稱相匹配。

當調用綁定到 ObjectDataSource 的控制項的 DataBind 方法時,這些控制項自動調用 SelectMethod 屬性指定的方法。

如果數據是作為 DataSet 或 DataTable 對象返回的,ObjectDataSource 控制項可以篩選由 SelectMethod 屬性檢索的數據。

  <asp:ObjectDataSource
    CacheDuration="string|Infinite"    CacheExpirationPolicy="Absolute|Sliding"
    CacheKeyDependency="string"
    ConflictDetection="OverwriteChanges|CompareAllValues"
    ConvertNullToDBNull="True|False"    DataObjectTypeName="string"
    DeleteMethod="string"    EnableCaching="True|False"
    EnablePaging="True|False"    EnableTheming="True|False"
    EnableViewState="True|False"    FilterExpression="string"
    ID="string"    InsertMethod="string"
    MaximumRowsParameterName="string"
    OldValuesParameterFormatString="string"
    OnDataBinding="DataBinding event handler"
    OnDeleted="Deleted event handler"    OnDeleting="Deleting event handler"
    OnDisposed="Disposed event handler"    OnFiltering="Filtering event handler"
    OnInit="Init event handler"    OnInserted="Inserted event handler"
    OnInserting="Inserting event handler"    OnLoad="Load event handler"
    OnObjectCreated="ObjectCreated event handler"
    OnObjectCreating="ObjectCreating event handler"
    OnObjectDisposing="ObjectDisposing event handler"
    OnPreRender="PreRender event handler"    OnSelected="Selected event handler"
    OnSelecting="Selecting event handler"    OnUnload="Unload event handler"
    OnUpdated="Updated event handler"    OnUpdating="Updating event handler"
    runat="server"    SelectCountMethod="string"
    SelectMethod="string"    SortParameterName="string"
    SqlCacheDependency="string"    StartRowIndexParameterName="string"
    TypeName="string"    UpdateMethod="string">
    <DeleteParameters>
                <asp:ControlParameter        ControlID="string"
                   ConvertEmptyStringToNull="True|False"
                    DefaultValue="string"
                   Direction="Input|Output|InputOutput|ReturnValue"
                    Name="string"
                    PropertyName="string"
                    Size="integer"
                    Type="Empty|Object|DBNull|Boolean|Char|SByte|
                        Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
                        Single|Double|Decimal|DateTime|String"
                />
                <asp:CookieParameter                 CookieName="string"/>
                <asp:FormParameter                   FormField="string"/>
                <asp:Parameter                       Name="string"/>
                <asp:ProfileParameter               PropertyName="string"/>
                <asp:QueryStringParameter      QueryStringField="string"/>
                <asp:SessionParameter          SessionField="string"/>
        </DeleteParameters>
        <FilterParameters>... ...</FilterParameters>
        <InsertParameters>... ...</InsertParameters>
        <SelectParameters>... ...</SelectParameters>
        <UpdateParameters>... ...</UpdateParameters>
  </asp:ObjectDataSource>