To implement this demo, Insus.net uses generic news examples, categories (directories), and articles. On a page, show all categories, and display the latest few news in each directory.
The effect is as follows:
The directory is displayed with the DataList control, and the article title list is a user control display, and the user control is dynamically added to the DataList.
Copy Code code as follows:
View Code
<asp:repeater id= "Repeater1" runat= "Server" onitemdatabound= "Repeater1_itemdatabound" >
<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 Code code as follows:
View Code
View Code
Imports System.Data
Imports insus.net
Partial Class Ascxcontrols_articlelist
Inherits System.Web.UI.UserControl
Implements isetvalue ' Inherit 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 Repeater control
Private Sub data_binding ()
Me.Repeater1.DataSource = _datasource
Me.Repeater1.DataBind ()
End Sub
Protected Sub Repeater1_itemdatabound (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 Code code 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);
}
}
Show Table of Contents:
Copy Code code as follows:
View Code
<asp:datalist id= "Datalistcatalog" runat= "Server" repeatcolumns= "2" repeatdirection= "Horizontal" "DataKeyField=" Catalog_nbr "onitemdatabound=" Datalistcatalog_itemdatabound ">
<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 gets data from the database and binds to the directory.
Copy Code code 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 next point is the Onitemdatabound event, in which you need to find the Asp:placeholder control, which will be used to load the user control.
Copy Code code 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 if asp:placeholder exists
if E.item.findcontrol ("Placeholderarticlelist") IsNot Nothing Then
Dim ctllaceholder as Placeholder = DirectCast (E.item.findcontrol ("Placeholderarticlelist"), Plac Eholder)
Dynamically loads the user control and converts it to an interface.
Dim Objuc as Isetvalue = DirectCast (LoadControl ("~/ascxcontrols/articlelist.ascx"), Isetvalue)
' Locate the directory primary key for 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 ())
loads the user control.
CtllaceHolder.Controls.Add (OBJUC)
End If
End If
End Sub