用Repeater的OnItemDataBound做master-detail

用Repeater的OnItemDataBound做master-detail

image

以前在asp時期這種東西應該大家都知道

跑兩個回圈就出來了

do while not rs.eof
select * from discuss_re where reid = rs("id")
do while not rs1.eof
rs1.movenext
loop
rs.movenext
loop

就好了

但是現在ASP.NET都改用資料繫結控制項了

初初轉到ASP.NET還真的會不知道該怎麼解

通常我會使用Repeater Web 伺服器控制來處理

處理這樣的需求重點就在於Repeater的OnItemDataBound事件

<asp:Repeater ID="Repeater1" runat="server" >
    <HeaderTemplate>
        <thead>
            <tr>
                <td>
                    OrderID </td>
                <td>
                    CustomerID </td>
                <td>
                    ShipName </td>
                <td>
                    ShipAddress </td>
                <td>
                </td>
            </tr>
        </thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("OrderID") %>
            </td>
            <td>
                <%# Eval("CustomerID") %>
            </td>
            <td>
                <%# Eval("ShipName") %>
            </td>
            <td>
                <%# Eval("ShipAddress") %>
            </td>
            <td>
                <asp:HiddenField ID="OrderID" Value='<%# Eval("OrderID") %>' runat="server" />
                <table border="1" style="border-collapse: collapse; width: 100%;">
                    <asp:Repeater ID="productRepeater" runat="server">
                        <HeaderTemplate>
                            <thead>
                                <td>
                                    <strong>Product Name</strong></td>
                            </thead>
                            <tbody>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <%# Eval("ProductName")%>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </tbody></FooterTemplate>
                    </asp:Repeater>
                </table>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

aspx的內容這樣處理,在ItemTemplate中另外在放一個Repeater跟HiddenField

HiddenField的value紀錄下要拿來select出detail的ID

Repeater1的DataSource我是在cs中處理

如下

string _connStr = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
using (SqlConnection _conn = new SqlConnection(_connStr)) {
    _conn.Open();
    SqlCommand _cmd = new SqlCommand(
        @"SELECT OrderID, CustomerID, ShipName, ShipAddress FROM Orders", _conn);
    DataTable _table = new DataTable("Orders");
    _table.Load(_cmd.ExecuteReader(CommandBehavior.SingleResult));
    Repeater1.DataSource = _table;
    Repeater1.DataBind();
}

 

然後幫Repeater1加上ItemDataBound事件

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">

並在cs中加上一點抓資料的code就完成了

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    HiddenField hidOrderID = e.Item.FindControl("OrderID") as HiddenField;
    Repeater productRepeater = e.Item.FindControl("productRepeater") as Repeater;
    if (hidOrderID != null && productRepeater != null) {
        using (SqlConnection _conn = new SqlConnection(_connStr)) {
            _conn.Open();
            SqlCommand _cmd = new SqlCommand(
                string.Format(
                    @"SELECT Products.ProductName
                FROM [Order Details] INNER JOIN
                Products ON [Order Details].ProductID = Products.ProductID
                WHERE [Order Details].OrderID = {0}", hidOrderID.Value), _conn);
            DataTable _table = new DataTable("Products");
            _table.Load(_cmd.ExecuteReader(CommandBehavior.SingleResult));
            productRepeater.DataSource = _table;
            productRepeater.DataBind();
        }
    }
}

主要是去e.Item.FindControl裡找server control來做處裡

有錯誤麻煩指正

感謝

補充 :

剛剛看到seanyhkao大大發的文章

用DataSet的Relation做master-detail

很好用歐,點部落真多有用的資料 :)