Datalist nesting (News lists are displayed for news categories)

Source: Internet
Author: User

(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 ();
}
}
}
}

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.