/* Add link */
// BackgroundCode
Sqlconnection CNN = New Sqlconnection ( "Server = (local); database = pubs; uid = sa; Pwd = qinfei ;" );
Sqldataadapter cmd1 = New Sqldataadapter ( "Select * from authors" , CNN );
Dataset DS = New Dataset ();
Statement 1.fill (DS, "Authors ");
Sqldataadapter cmd2 = new sqldataadapter ( "Select * From titleauthor" , CNN );
Limit 2.fill (DS, "Titles" );
DS. relations. Add ( "Myrelation" , DS. Tables [ "Authors" ]. Columns [ "Au_id" ], DS. Tables [ "Titles" ]. Columns [ "Au_id" ]);
Parent. datasource = Ds. Tables [ "Authors" ];
Page. databind ();
CNN. Close ();
// Page code
<! -- Parent control -->
<Asp: repeater id = "parent" runat = "server">
<Itemtemplate>
<B> <% # databinder. eval (container. dataitem, "au_id") %> </B> <br>
<! -- Subcontrol --> <! -- Bind the data source and create a myrelation relationship in the background -->
<Asp: repeater id = "child" datasource =' <% # (Datarowview) container. dataitem). Row. getchildrows ("myrelation ") %> 'Runat = "server">
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "[\" title_id \ "]") %> <Br> <! -- When the child control binds data and calls data, the row parameter in the method must be added with ["] -->
</Itemtemplate>
</ASP: repeater>
<! -- Subcontrol -->
</Itemtemplate>
</ASP: repeater>
<! -- Parent control -->
/* Bind a data source to the Child control */
// Background code
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
Binddata ();
}
// Obtain data
Private void binddata ()
{
String sqlstmt = "select * from MERs MERS; select * from orders ";
String constring = "Server = localhost; database = northwind; uid = sa; Pwd = ;";
Sqldataadapter SDA = new sqldataadapter (sqlstmt, constring );
SDA. fill (DS);
DS. tables [0]. tablename = "customers";
DS. tables [1]. tablename = "orders";
Datagrid1.datasource = Ds. Tables ["MERs"];
Datagrid1.databind ();
}
// Obtain the number of returned results, which can be directly called in the Child control on the foreground page
Protected dataview getordersdatasource (string _ customerid)
{
Dataview _ orders = Ds. Tables ["orders"]. defaultview;
_ Orders. rowfilter = "customerid = '" + _ customerid + "'";
Return _ orders;
}
// Same as above, without Parameters
Protected dataview getordersperformance1 ()
{
Dataview _ orders = Ds. Tables ["orders"]. defaultview;
_ Orders. rowfilter = "customerid = 'bsbev '";
Return _ orders;
}
// Page code
<! -- Parent control -->
<Asp: DataGrid id = "datagrid1" runat = "server">
<Columns>
<Asp: templatecolumn itemstyle-verticalalign = "TOP" headertext = "orders" itemstyle-width = "100%">
<Itemtemplate>
Parent Control <% # Databinder. eval (container. dataitem, "customerid ") %>
<! -- Subcontrol -->
<Asp: DataGrid runat = "server" autogeneratecolumns = "false" width = "100%" datasource ='<%#Getordersperformance1 () %> 'Id = "maid" name = "maid">
<Columns>
<Asp: boundcolumn datafield = "orderid" headertext = "Order ID"/> <asp: templatecolumn>
<Itemtemplate> <% # Databinder. eval (container. dataitem, "orderdate", "sub-control = {0 }") %> </Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>
<! -- Subcontrol -->
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>
<! -- Parent control -->