为了实现这个演示,Insus.NET使用通用的新闻例子,它类别(目录)以及文章。在一个页面,显示所有类别,每个目录下显示最新几条新闻。
效果如下:
目录是用DataList控件显示,而文章标题列表是一个用户控件显示,这个用户控件将动态被加入至DataList。
复制代码 代码如下:
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>
复制代码 代码如下:
View Code
View Code
Imports System.Data
Imports Insus.NET
Partial Class AscxControls_ArticleList
Inherits System.Web.UI.UserControl
Implements ISetValue '继承接口
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
'绑定数据至Repeater控件
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
'实现接口
Public Sub SetValue(str As Object) Implements ISetValue.SetValue
Me._DataSource = str
End Sub
End Class
上面用户控件中,有一个接口:
复制代码 代码如下:
ISetValue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Insus.NET
{
public interface ISetValue
{
void SetValue(object obj);
}
}
显示目录:
复制代码 代码如下:
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>
从数据库获取数据并绑定至目录的DataList控件。
复制代码 代码如下:
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
下面是重点,就是OnItemDataBound事件,在这个事件中,需要找到asp:PlaceHolder控件,这个容器将用来加载用户控件。
复制代码 代码如下:
Protected Sub DataListCatalog_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
'判断asp:PlaceHolder是否存在
If e.Item.FindControl("PlaceHolderArticleList") IsNot Nothing Then
Dim ctllaceHolder As PlaceHolder = DirectCast(e.Item.FindControl("PlaceHolderArticleList"), PlaceHolder)
'动态加载用户控件,并转换为接口。
Dim objuc As ISetValue = DirectCast(LoadControl("~/AscxControls/ArticleList.ascx"), ISetValue)
'找到DataList控件的目录主键
objArticle.Catalog_nbr = Me.DataListCatalog.DataKeys(e.Item.ItemIndex)
objArticle.Top = 2
'为用户控件赋值。
objuc.SetValue(objArticle.GetArticalTopByCatalog())
'加载用户控件。
ctllaceHolder.Controls.Add(objuc)
End If
End If
End Sub
效果如下:
目录是用DataList控件显示,而文章标题列表是一个用户控件显示,这个用户控件将动态被加入至DataList。
复制代码 代码如下:
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>
复制代码 代码如下:
View Code
View Code
Imports System.Data
Imports Insus.NET
Partial Class AscxControls_ArticleList
Inherits System.Web.UI.UserControl
Implements ISetValue '继承接口
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
'绑定数据至Repeater控件
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
'实现接口
Public Sub SetValue(str As Object) Implements ISetValue.SetValue
Me._DataSource = str
End Sub
End Class
上面用户控件中,有一个接口:
复制代码 代码如下:
ISetValue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Insus.NET
{
public interface ISetValue
{
void SetValue(object obj);
}
}
显示目录:
复制代码 代码如下:
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>
从数据库获取数据并绑定至目录的DataList控件。
复制代码 代码如下:
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
下面是重点,就是OnItemDataBound事件,在这个事件中,需要找到asp:PlaceHolder控件,这个容器将用来加载用户控件。
复制代码 代码如下:
Protected Sub DataListCatalog_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
'判断asp:PlaceHolder是否存在
If e.Item.FindControl("PlaceHolderArticleList") IsNot Nothing Then
Dim ctllaceHolder As PlaceHolder = DirectCast(e.Item.FindControl("PlaceHolderArticleList"), PlaceHolder)
'动态加载用户控件,并转换为接口。
Dim objuc As ISetValue = DirectCast(LoadControl("~/AscxControls/ArticleList.ascx"), ISetValue)
'找到DataList控件的目录主键
objArticle.Catalog_nbr = Me.DataListCatalog.DataKeys(e.Item.ItemIndex)
objArticle.Top = 2
'为用户控件赋值。
objuc.SetValue(objArticle.GetArticalTopByCatalog())
'加载用户控件。
ctllaceHolder.Controls.Add(objuc)
End If
End If
End Sub
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]