(The corresponding News list is displayed in the news category), first:
This requires datalist nesting! The idea is simple! First, bind the parent datalist. Next, it is important to bind the itemdatabound event of the parent datalist to the news list of its subordinates. This is mainly to get the ID of the parent datalist! Below isCode! Study it by yourself!
Code
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
// Bind datalistnewcat
Dataset dsnewcat = Hy. sqlhelper. sqlhelper. executedataset ( " Newcat " , Commandtype. Text,
" Select * From hy_new_cat order by cat_id DESC " );
Dlnewcat. datasource = Dsnewcat. Tables [ " Newcat " ]. Defaultview;
Dlnewcat. databind ();
Dsnewcat. Dispose ();
}
}
// Bind datalistnewtitle
Protected Void Dlnewcat_itemdatabound ( Object Sender, datalistitemeventargs E)
{
If (E. Item. itemtype = Listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem)
{
Datalist dlnewtitle = (Datalist) E. Item. findcontrol ( " Dlnewtitle " );
Datarowview DRV = (Datarowview) E. Item. dataitem;
Int Catid = Convert. toint32 (DRV [ " Cat_id " ]);
If (Catid > 0 )
{
Dataset dsnewtitle = Hy. sqlhelper. sqlhelper. executedataset ( " Newtitle " , Commandtype. Text,
" Select top 5 new_id, new_top, new_title, new_date from hy_new where cat_id = @ catid order by new_top ASC, new_id DESC " ,
New Sqlparameter ( " @ Catid " , Catid ));
If (Dsnewtitle ! = Null )
{
Dlnewtitle. datasource=Dsnewtitle. Tables ["Newtitle"]. Defaultview;
Dlnewtitle. databind ();
}
}
}
}