Asp.net control nesting

Source: Internet
Author: User

/* 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 -->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.