動態(tài)加載用戶控件至DataList并為用戶控件賦值實(shí)例演示
更新時間:2013年01月10日 16:38:51 作者:
本文借用使用通用的新聞例子演示動態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下
為了實(shí)現(xiàn)這個演示,Insus.NET使用通用的新聞例子,它類別(目錄)以及文章。在一個頁面,顯示所有類別,每個目錄下顯示最新幾條新聞。
效果如下:
目錄是用DataList控件顯示,而文章標(biāo)題列表是一個用戶控件顯示,這個用戶控件將動態(tài)被加入至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
'綁定數(shù)據(jù)至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
'實(shí)現(xiàn)接口
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>
從數(shù)據(jù)庫獲取數(shù)據(jù)并綁定至目錄的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
下面是重點(diǎn),就是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)
'動態(tài)加載用戶控件,并轉(zhuǎn)換為接口。
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
效果如下:

復(fù)制代碼 代碼如下:
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>
復(fù)制代碼 代碼如下:
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
'綁定數(shù)據(jù)至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
'實(shí)現(xiàn)接口
Public Sub SetValue(str As Object) Implements ISetValue.SetValue
Me._DataSource = str
End Sub
End Class
上面用戶控件中,有一個接口:
復(fù)制代碼 代碼如下:
ISetValue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Insus.NET
{
public interface ISetValue
{
void SetValue(object obj);
}
}
顯示目錄:
復(fù)制代碼 代碼如下:
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>
從數(shù)據(jù)庫獲取數(shù)據(jù)并綁定至目錄的DataList控件。
復(fù)制代碼 代碼如下:
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
下面是重點(diǎn),就是OnItemDataBound事件,在這個事件中,需要找到asp:PlaceHolder控件,這個容器將用來加載用戶控件。
復(fù)制代碼 代碼如下:
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)
'動態(tài)加載用戶控件,并轉(zhuǎn)換為接口。
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
相關(guān)文章
asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中
asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中的實(shí)現(xiàn)代碼。2009-05-05Asp.Net?Core7?preview4限流中間件新特性詳解
這篇文章主要為大家介紹了Asp.Net?Core7?preview4限流中間件的新特性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05xls表格導(dǎo)入數(shù)據(jù)庫功能實(shí)例代碼
這篇文章介紹了xls表格導(dǎo)入數(shù)據(jù)庫功能實(shí)例代碼,有需要的朋友可以參考一下2013-10-10在ASP.NET中實(shí)現(xiàn)彈出日歷的具體方法
這篇文章介紹了ASP.NET彈出日歷功能的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-07-07ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證,感興趣的小伙伴們可以參考一下2016-07-07.NET Core 基于Websocket的在線聊天室實(shí)現(xiàn)
這篇文章主要介紹了.NET Core 基于Websocket的在線聊天室實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03