When making a singer list, I want to make it according to the first letters of the pinyin. Because the singer is put together, it involves the problem of recorded nested, I found it online, I found that I didn't know very well, after a study With the following experience: Datalist's nest (first explain, DLOUTSIDE means the DataList in the outside, the implementation principle is when DLOUTSIDE is created, so the dynamically generates DLINNER, so, DLOUTSIDE is defined, Define a function, trigger when DloutSide creates, is a function pointing to DloutSide.Itemcreated. Here, we assume that this function is DLOUTSIDEITEMCREATED (). In DLOUTSIDEITEMCREATED (), based on E.Item.itemType, the required thing is generated, if (E.Item.ItemType == ListItemType.Item.Item) means when Dloutside creates a normal item, E.Item.itemType == ListiteMTYPE.AlternatingItem is Create a cross-replacement item. Next, it is the focus. Dlinner's data source is obtained according to DLOUTSIDE.DATAKEYS [E.Item.ItemIndex], so DLOUTSIDE's DATAKEYS is the key to contact. Dlinner is to be generated dynamically, so we have to write a class yourself, inherit itemplate, this is a template. There is instantiatein () when establishing a template (), this is the main part of the generation template, the data is bound, so in instantiatein (), you want to define a trigger event, bind data in the triggered event.
The principle is almost the same, here I will write an example, give you a deeper image, because it is a demonstration, it is not very specific, and it is necessary to rely on everyone: Private void Dloutside_Itemcreated (Object Sender, System.Web.ui.WebControls .DataListItemEventArgs e) {if (e.Item.ItemType == ListItemType.Item) {Literal lc = new Literal (); DataList DLInner = new DataList (); DLInner.RepeatDirection = RepeatDirection.Horizontal; DLInner.RepeatColumns = 5; DLInner .Itemtemplate = new mytemp (); // call custom template DBObject obj2 = new dbobject (); obj2.selectstring = "select name from singer where initial = '" DLOUTSIDE.DATAKEYS [E.Item.ItemIndex] " "//, call my own database operation class, in the previous article, Dlinner.DataSource = Obj2.dw; dlinner.database (); E.Item.controls.add (dlinner); OBJ2 .Dbclose ();} if (E.Item.itemType == Listitemt Ype.alternatingItem) {// This is not written here;}} Dloutside is here, below is a custom template for Dlinner: public class mytemp: itemplate {public void instantiatein (System.Web.ui.Control Container) {Litral LC = new literal (); lc.databinding = new eventHandler (this.templatecontrol_database; container.controls.add (lc);