Dynamically create repeater and bind data

Source: Internet
Author: User

There is a menu tree, because the interface restrictions (requirements) cannot use existing controls.

Then I thought of using the Repeater control to write it myself, so that the page can be controlled flexibly and conveniently.

 

< ASP: Repeater ID = "Categorylist" Runat = "Server" Onitemdatabound = "Categorylist_itemdatabound" >
< Headertemplate >
< Div Style = "Width: 90%; text-align: Left; padding-left: 10px ;" >
</ Headertemplate >
< Itemtemplate >
< Div Style = "Line-Height: 30px ;" >
< IMG SRC = "Images/jt_red.gif" Width = "3" Height = "5"   /> < IMG SRC = "Images/jt_red.gif"
Width = "3" Height = "5"   />
< ASP: hyperlink ID = "Hlrootcategory" Cssclass = "DH" Runat = "Server" > Category </ ASP: hyperlink >
</ Div >

</ Itemtemplate >
< Footertemplate >
</ Div >
</ Footertemplate >
</ ASP: Repeater >
The data source control is not used here. Data is dynamically bound in the background. Protected   Void Page_load ( Object Sender, eventargs E)
{
Dataset DS =   New Aricc. ooxx. BLL. Category (). getlist ( " Parentid = 0 " );
Categorylist. datasource = DS;
Categorylist. databind ();
}

Protected   Void Categorylist_itemdatabound ( Object Sender, repeateritemeventargs E)
{
Datarowview DRV = (Datarowview) E. Item. dataitem;
Hyperlink Link = (Hyperlink) E. Item. findcontrol ( " Hlrootcategory " );
If (Link ! =   Null )
{
Link. Text = DRV [ " Categoryname " ]. Tostring ();
Link. navigateurl =   " .../Search. aspx? C = "   + DRV [ " ID " ]. Tostring ();

Listinnercategory (E. Item, Int . Parse (DRV [ " ID " ]. Tostring ()));
}
}
Do you see the listinnercategory method? It is a method for recursively displaying subclass data. In this method, the repeater control is dynamically created and bound to the corresponding data. To dynamically create a repeater control, you must define a template. Used to assign values to the itemtemplate of repeater. This template must be an instance of a class that implements the itemplate interface. Next we will first define such a template class Public   Class Categorytemplate: itemplate
{

# region itemplate member
private int currentlevel;

Public categorytemplate ( int level)
{< br> currentlevel = level;
}< br> Public void instantiatein (control container)
{

Hyperlink Link= NewHyperlink ();
Link. ID= "Innerlink";

Htmlgenericcontrol Div =   New Htmlgenericcontrol ();
Div. tagname =   " Div " ;
Div. Attributes. Add ( " Style " , " Line-Height: 30px; text-indent: "   + (Currentlevel *   20 ) +   " Px; " );
Div. ID =   " Innerdiv " ;
Div. Controls. Add (Link );

Container. Controls. Add (DIV );
}

# Endregion
}
The following is the definition of listinnercategory and corresponding event processing. Program Private   Int Currentlevel =   0 ;
Private   Void Listinnercategory (repeateritem item, Int ID)
{
If ( New Aricc. ooxx. BLL. Category (). haschild (ID ))
{
Currentlevel ++ ;
Dataset DS =   New Aricc. ooxx. BLL. Category (). getlist ( " Parentid = "   + ID );
Repeater rep =   New Repeater ();
Categorytemplate Template =   New Categorytemplate (currentlevel );

rep. itemtemplate = template;
rep. itemdatabound += New repeateritemeventhandler (rep_itemdatabound );

Rep. datasource=DS;
Rep. databind ();
Item. Controls. Add (REP );
Currentlevel--;

}
}

Void Rep_itemdatabound ( Object Sender, repeateritemeventargs E)
{
Datarowview DRV = (Datarowview) E. Item. dataitem;
Hyperlink Link = (Hyperlink) E. Item. findcontrol ( " Innerlink " );
If (Link ! =   Null )
{
Link. Text = DRV [ " Categoryname " ]. Tostring ();
Link. navigateurl =   " .../Search. aspx? C = "   + DRV [ " ID " ]. Tostring ();

listinnercategory (E. item, int . parse (DRV [ " id " ]. tostring ();
}< BR >}< br> at this point, a multi-level tree menu is displayed. Maybe my application here is not a good solution. I wrote this Article to illustrate the dynamic creation of repeater. As mentioned in the question. Hope to help you.

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.