To achieve this demonstration, Insus. NET uses a general news example, including its category (directory) and article. On a page, all categories are displayed, and the latest news is displayed in each directory.
The effect is as follows:
The directory is displayed using the DataList control, and the article title list is displayed by a user control. This user control is dynamically added to DataList.Copy codeThe Code is as follows: View Code
<Asp: Repeater ID = "Repeater1" runat = "server" OnItemDataBound = "repeaterincluitemdatabound">
<HeaderTemplate>
<Table cellpadding = "5" cellspacing = "0" width = "100%">
</HeaderTemplate>
<ItemTemplate>
<Tr style = "height: 30px; line-height: 10px;">
<Td>
$
</Td>
<Td>
<Asp: HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = '<% #"~ /ArticleView. aspx? ID = "& Eval (" Article_nbr ") %> 'tooltip = '<% # Eval (" Subject ") %> 'target =" _ blank "> </asp: hyperLink>
</Td>
<Td>
<% # ObjInsusDateTimeUtility. GetDateTime (Eval ("PublicDate"), "yyyy-MM-dd") %>
</Td>
</Tr>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</Asp: Repeater>
Copy codeThe Code is as follows: View Code
View Code
Imports System. Data
Imports Insus. NET
Partial Class AscxControls_ArticleList
Inherits System. Web. UI. UserControl
Implements isetvalue' inherited Interface
Dim objArticle As New Article ()
Protected objInsusDateTimeUtility As New InsusDateTimeUtility ()
Private _ DataSource As Object
Private _ SubjectLength As Integer = 20
Public WriteOnly Property SubjectLength () As Integer
Set (ByVal value As Integer)
_ SubjectLength = value
End Set
End Property
Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs) Handles Me. Load
Data_Binding ()
End Sub
'Bind data to the Repeater control
Private Sub Data_Binding ()
Me. Repeater1.DataSource = _ DataSource
Me. Repeater1.DataBind ()
End Sub
Protected Sub repeaterincluitemdatabound (ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
Dim objDrv As DataRowView = DirectCast (e. Item. DataItem, DataRowView)
If e. Item. ItemType = ListItemType. AlternatingItem OrElse e. Item. ItemType = ListItemType. Item Then
If e. Item. FindControl ("HyperLink1") IsNot Nothing Then
Dim LinkSubject As HyperLink = DirectCast (e. Item. FindControl ("HyperLink1"), HyperLink)
If objDrv ("Subject"). Length> _ SubjectLength Then
LinkSubject. Text = objDrv ("Subject"). Substring (0, _ SubjectLength )&"..."
Else
LinkSubject. Text = objDrv ("Subject"). ToString ()
End If
End If
End If
End Sub
'Implementation Interface
Public Sub SetValue (str As Object) Implements ISetValue. SetValue
Me. _ DataSource = str
End Sub
End Class
In the above user control, there is an interface:Copy codeThe Code is as follows: ISetValue
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Insus. NET
{
Public interface ISetValue
{
Void SetValue (object obj );
}
}
Display directory:Copy codeThe Code is as follows: View Code
<Asp: DataList ID = "DataListCatalog" runat = "server" RepeatColumns = "2" RepeatDirection = "Horizontal" DataKeyField = "Catalog_nbr" OnItemDataBound = "external">
<ItemTemplate>
<Div style = "padding: 2PX; height: 25px; background-color: # cbfb25; font-weight: bold; line-height: 25PX;">
<% # Eval ("CatalogName") %>
</Div>
<Asp: PlaceHolder ID = "PlaceHolderArticleList" runat = "server"> </asp: PlaceHolder>
</ItemTemplate>
</Asp: DataList>
The DataList control that retrieves data from the database and binds it to the directory.Copy codeThe Code is as follows: View Code
Imports Insus. NET
Partial Class Index
Inherits System. Web. UI. Page
Dim objCatalog As New Catalog ()
Dim objArticle As New Article ()
Protected Sub Page_Load (sender As Object, e As EventArgs) Handles Me. Load
If Not IsPostBack Then
Data_Binding ()
End If
End Sub
Private Sub Data_Binding ()
ObjCatalog. IsActive = True
Me. DataListCatalog. DataSource = objCatalog. GetByIsActive ()
Me. DataListCatalog. DataBind ()
End Sub
End Class
The following is the OnItemDataBound event. In this event, you need to find the asp: PlaceHolder control, which is used to load the user control.Copy codeThe Code is As follows: Protected Sub DataListCatalog_ItemDataBound (sender As Object, e As DataListItemEventArgs)
If e. Item. ItemType = ListItemType. Item OrElse e. Item. ItemType = ListItemType. AlternatingItem Then
'Determine whether asp: PlaceHolder exists
If e. Item. FindControl ("PlaceHolderArticleList") IsNot Nothing Then
Dim ctllaceHolder As PlaceHolder = DirectCast (e. Item. FindControl ("PlaceHolderArticleList"), PlaceHolder)
'Dynamically load user controls and convert them to interfaces.
Dim objuc As ISetValue = DirectCast (LoadControl ("~ /AscxControls/ArticleList. ascx "), ISetValue)
'Locate the directory primary key of the DataList Control
ObjArticle. Catalog_nbr = Me. DataListCatalog. DataKeys (e. Item. ItemIndex)
ObjArticle. Top = 2
'Assigns a value to the user control.
Objuc. SetValue (objArticle. GetArticalTopByCatalog ())
'Load the user control.
CtllaceHolder. Controls. Add (objuc)
End If
End If
End Sub