Ado. Net nested Data Binding

Source: Internet
Author: User
Automatically obtains master-slave relationship data in the database. The following lists the cities that match the province and province, Code As follows:

< ASP: Repeater ID = "Repeater1" Runat = "Server" >
< Itemtemplate >
< Span Style = "Font-weight: bold" > <% #Eval("Pname") %> </ Span >
< Ul >
< ASP: Repeater ID = "Detal" Runat = "Server" Datasource = '<% # Getcity (eval ("PID"). tostring ()) % > '>
< Itemtemplate >
< Li > <% #Eval("City_name") %> </ Li >  
</ Itemtemplate >
</ ASP: Repeater >
</ Ul >
</ Itemtemplate >
</ ASP: Repeater >

Important: datasource = '<% # getawd (eval ("PID"). tostring () %>' Call the getcity method to obtain the subclass.

Protected   Void Page_load ( Object Sender, eventargs E)
{
If(!Page. ispostback)
Bindgrid ();
}
// Obtain a category
Protected   Void Bindgrid ()
{
String Plain text =   " Select PID, pname from Pro " ;
Datatable dt = VC. getdatatable (plain text );
Repeater1.datasource = DT;
Repeater1.databind ();
}
// Get small content
Protected Datatable getcity ( String ID)
{
String Plain text =   " Select city_name from city where promo_id = "   + ID +   "" ;
Datatable dt = VC. getdatatable (plain text );
Return DT;
}

The implementation result is as follows:
 

Method 2: bind the second repeater to the itemdatabound event of the first repeater. The first repeater in HTML code is not assignedDatasource

Protected   Void Repeattitle_itemdatabound ( Object Sender, repeateritemeventargs E)
{

Datarowview Dr = (Datarowview) E. Item. dataitem;
Datatable mydt = Getcity (Dr [ 0 ]. Tostring ());
If (Mydt. Rows. Count >   0 )
{
Repeater myrepeat = (Repeater) E. Item. findcontrol ( " Repeatcont " );
Myrepeat. datasource = Mydt;
Myrepeat. databind ();
Mydt. Dispose ();
}
Else
{
E. Item. Visible= False;//No data is displayed
}
}

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.