[C#.NET][ASP.NET] Use ObjectDataSource of Session Achieve Master-Detail at GridView

[C#.NET][ASP.NET] Use ObjectDataSource of Session Achieve Master-Detail at GridView

續上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/11/13/147293.aspx

上篇演練了 ObjectDataSource Binding,本篇要演練 Master-Detail

 

首先建立資料結構,Order、OrderItem,Order.GetOrders 靜態方法用來建立 Order 集合(假資料)

完整程式碼如下:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Models/Order.cs

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Models/OrderItem.cs

 

然後準備 OrderUseCase 類別讓 ObjectDataSource 接收,實作步驟參考以下:

http://www.dotblogs.com.tw/yc421206/archive/2014/11/13/147293.aspx#@EmployeeDataAccess.cs%EF%BC%8C%E8%A8%AD%E5%AE%9A%20DAO%20%E7%89%A9%E4%BB%B6

 

image

 

完整程式碼如下:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Controls/OrderUseCase.cs

 

UI規劃如下圖:

Order_ObjectDataSource.SelectMethod="GetAllOrders”

OrderItem_ObjectDataSource.SelectMethod="GetAllOrderItems”

 

Order_GridView 綁定 Order_ObjectDataSource

OrderItem_GridView 綁定 OrderItem_ObjectDataSource

image

 

@Order_ObjectDataSource

這部份的設定跟上篇相同,在這篇就不再贅述

 

@OrderItem_ObjectDataSource

image

image

image

 

比較不一樣的是,當 ObjectDataSource 選擇了帶有參數的方法,就會跳出以下視窗,在這裡我選用 Session,定義 SessionField=”SESSION_CURRENT_ORDER”

 

image

 

@WebForm1.aspx

在設計工具設定好 Order_ObjectDataSource & OrderItem_ObjectDataSource 後,VS2013 就幫我產生以下

SessionParameter Name 就是 OrderUseCase.GetAllOrderItems 方法裡的參數名稱

SessionField 就是 Session Name,接下來我們需要手動處理 Session

<asp:ObjectDataSource ID="Order_ObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetAllOrders"
    TypeName="Simple.ObjectDataSourceSession.Controls.OrderUseCase"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="OrderItem_ObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetAllOrderItems"
    TypeName="Simple.ObjectDataSourceSession.Controls.OrderUseCase">
    <SelectParameters>
        <asp:SessionParameter Name="orderNo" SessionField="SESSION_CURRENT_ORDER" DbType="Guid" />
    </SelectParameters>
</asp:ObjectDataSource>

 

完整程式碼如下:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Views/WebForm1.aspx

 

點選 Master 資料列,改變 Detail

當我在 Order_GridView(Master) 選擇任何一 筆資料時,我需要觸發 Order_GridView_SelectedIndexChanged 事件,它用來取得選擇列的 orderNo,並將取得結果存放至 Session["SESSION_CURRENT_ORDER"]

@WebForm1.aspx.cs

我將會用到 Order_GridView.SelectValue 屬性,所以需要設定 Order_GridView.DataKeyNames 屬性

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //GridView要定義DataKeyNames,SelectValue屬性才會有資料
        this.Order_GridView.DataKeyNames = new[] { "OrderNo" };
    }
}

 

Order_GridView.SelectValue 只適合處理一個欄位,若需要處理多個欄位,就要利用 Order_GridView.SelectRow 來處理

protected void Order_GridView_SelectedIndexChanged(object sender, EventArgs e)
{
    if (sender is GridView == false)
    {
        return;
    }
    var gridView = (GridView)sender;
    //SelectValue不支援多欄位
    var selectGuid = gridView.SelectedValue;
    this.Session["SESSION_CURRENT_ORDER"] = selectGuid;

    //var selectRow = gridView.SelectedRow;
    //var orderNo = selectRow.Cells[0].Text;
    //this.Session["SESSION_CURRENT_ORDER"] = orderNo;
}

 

完整程式碼如下:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Views/WebForm1.aspx.cs

 

@WebForm1.aspx

<asp:GridView ID="Order_GridView" runat="server" AutoGenerateColumns="False" DataSourceID="Order_ObjectDataSource" 
    OnSelectedIndexChanged="Order_GridView_SelectedIndexChanged">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="OrderNo" HeaderText="OrderNo" SortExpression="OrderNo" />
        <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
        <asp:BoundField DataField="SupplierId" HeaderText="SupplierId" SortExpression="SupplierId" />
        <asp:BoundField DataField="SupplierName" HeaderText="SupplierName" SortExpression="SupplierName" />
    </Columns>
</asp:GridView>

 

完整程式碼如下:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/Simple.ObjectDataSourceSession/Views/WebForm1.aspx

完成後便完成 Master-Detail 連動

 


 

文章出自:http://www.dotblogs.com.tw/yc421206/archive/2014/11/23/147393.aspx

範例下載:https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceSession/

 

 

 

 

 

 

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo