欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

动态加载用户控件至DataList并为用户控件赋值实例演示

程序员文章站 2024-03-05 20:46:43
为了实现这个演示,insus.net使用通用的新闻例子,它类别(目录)以及文章。在一个页面,显示所有类别,每个目录下显示最新几条新闻。 效果如下: 目录是用datalist...
为了实现这个演示,insus.net使用通用的新闻例子,它类别(目录)以及文章。在一个页面,显示所有类别,每个目录下显示最新几条新闻。

效果如下:
动态加载用户控件至DataList并为用户控件赋值实例演示目录是用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