How to use multiple repeater in. net

Source: Internet
Author: User

Front-end page:

< ASP: Repeater ID = "Rptcategories" Runat = "Server" >  
< Headertemplate >  
< Table Width = "100%" Border = "0" Cellspacing = "0" Cellpadding = "0" >  
</ Headertemplate >  
< Itemtemplate >  
<! -- Category name -->  
< Tr > < Th > <% # Databinder.Eval(Container. dataitem,"Typename") %> </ Th > </ Tr >  
<! -- Products under category -->  
< ASP: Repeater ID = "Rptproduct" Runat = "Server" >  
< Itemtemplate >  
< Tr > < TD > < A Href = 'Productinfo. aspx? Id = <% # Databinder. eval (container. dataitem, "ID") % > '> <% # Databinder.Eval(Container. dataitem,"Productname") %> </ A > </ TD > </ Tr >  
</ Itemtemplate >  
</ ASP: Repeater >  
</ Itemtemplate >  
< Footertemplate >  
</ Table >  
</ Footertemplate >  
</ ASP: Repeater >  

BackgroundCodePart (part of code ):

// When you bind a category name to a product under the category  
Private   Void Rptcategories_itemdatabound ( Object Sender, system. Web. UI. webcontrols. repeateritemeventargs E)
{
Bll. Products = New Bll. Products ();
If (E. Item. itemtype = Listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem)
{
Repeater rptproduct = (Repeater) E. Item. findcontrol ( " Rptproduct " );
// Locate the data item associated with the category Repeater  
Datarowview rowv = (Datarowview) E. Item. dataitem;
// Extract category ID  
Int Categorieid = Convert. toint32 (rowv [ " ID " ]);
// Query the products under the category based on the category ID, and bind the product Repeater  
Rptproduct. datasource = Products. getproductsbycategorieid (categorieid );
Rptproduct. databind ();
}  
}  

Another method:

Front-end:

<! -- Start parent repeater -->
<Asp: repeater id = "parentrepeater" runat = "server">
<Itemtemplate>
<B> <% # databinder. eval (container. dataitem, "au_id") %> </B> <br>

<! -- Start child repeater -->
<Asp: repeater id = "childrepeater" datasource = '<% # (datarowview) container. dataitem)
. Row. getchildrows ("myrelation") %> 'runat = "server">

<Itemtemplate>
<% # Databinder. eval (container. dataitem, "[\" title_id \ "]") %> <br>
</Itemtemplate>
</ASP: repeater>
<! -- End Child repeater -->

</Itemtemplate>
</ASP: repeater>
<! -- End parent repeater -->

 

Background: public partial class nestedrepeater: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Repeater childrepeater; // parentrepeater;
Public nestedrepeater ()
{
Page. init + = new system. eventhandler (page_init );
}
Public void page_load (Object sender, eventargs E)
{
// Create the connection and dataadapter for the authors table.
Sqlconnection CNN = new sqlconnection ("Server =.; database = pubs; user id = sa; Pwd = ;");
Sqldataadapter cmd1 = new sqldataadapter ("select * from authors", CNN );

// Create and fill the dataset.
Dataset DS = new dataset ();
Statement 1.fill (DS, "Authors ");

// Create a second dataadapter for the titles table.
Sqldataadapter cmd2 = new sqldataadapter ("select * From titleauthor", CNN );
Listen 2.fill (DS, "titles ");

// Create the relation bewtween the authors and titles tables.
DS. relations. Add ("myrelation ",
DS. Tables ["Authors"]. Columns ["au_id"],
DS. Tables ["titles"]. Columns ["au_id"]);

// bind the authors table to the parent Repeater control, and call databind.
parentrepeater. datasource = Ds. tables ["Authors"];
page. databind ();

// Close the connection.
CNN. Close ();
}
Private void page_init (Object sender, eventargs E)
{
Initializecomponent ();
}
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}

}

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.