大家好,欢迎来到IT知识分享网。
有一个菜单树,因为界面上的限制(要求)不能使用现有的控件。
那我就想到了用Repeater控件自己来写,可以灵活、方便的控制页面。
asp:Repeater
ID
=”CategoryList”
runat
=”server”
OnItemDataBound
=”CategoryList_ItemDataBound”
>
<
HeaderTemplate
>
<
div
style
=”width: 90%; text-align:left; padding-left:10px;”
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
div
style
=”line-height:30px;”
>
<
img
src
=”images/jt_red.gif”
width
=”3″
height
=”5″
/><
img
src
=”images/jt_red.gif”
width
=”3″
height
=”5″
/>
<
asp:HyperLink
ID
=”hlRootCategory”
CssClass
=”dh”
runat
=”server”
>
Category
</
asp:HyperLink
>
</
div
>
</
ItemTemplate
>
<
FooterTemplate
>
</
div
>
</
FooterTemplate
>
</
asp:Repeater
>
protected
void
Page_Load(
object
sender, EventArgs e)
{
DataSet ds
=
new
Aricc.ooxx.BLL.Category().GetList(
“
parentid=0
“
);
CategoryList.DataSource
=
ds;
CategoryList.DataBind();
}
protected
void
CategoryList_ItemDataBound(
object
sender, RepeaterItemEventArgs e)
{
DataRowView drv
=
(DataRowView)e.Item.DataItem;
HyperLink link
=
(HyperLink)e.Item.FindControl(
“
hlRootCategory
“
);
if
(link
!=
null
)
{
link.Text
=
drv[
“
categoryname
“
].ToString();
link.NavigateUrl
=
“
../search.aspx?c=
“
+
drv[
“
id
“
].ToString();
ListInnerCategory(e.Item,
int
.Parse(drv[
“
id
“
].ToString()));
}
}
public
class
CategoryTemplate : ITemplate
{
#region
ITemplate 成员
private
int
currentLevel;
public
CategoryTemplate(
int
level)
{
currentLevel
=
level;
}
public
void
InstantiateIn(Control container)
{
HyperLink link
=
new
HyperLink();
link.ID
=
“
innerLink
“
;
HtmlGenericControl div
=
new
HtmlGenericControl();
div.TagName
=
“
div
“
;
div.Attributes.Add(
“
style
“
,
“
line-height:30px;text-indent:
“
+
(currentLevel
*
20
)
+
“
px;
“
);
div.ID
=
“
innerDiv
“
;
div.Controls.Add(link);
container.Controls.Add(div);
}
#endregion
}
private
int
currentLevel
=
0
;
private
void
ListInnerCategory(RepeaterItem item,
int
ID)
{
if
(
new
Aricc.ooxx.BLL.Category().HasChild(ID))
{
currentLevel
++
;
DataSet ds
=
new
Aricc.ooxx.BLL.Category().GetList(
“
parentid=
“
+
ID);
Repeater rep
=
new
Repeater();
CategoryTemplate template
=
new
CategoryTemplate(currentLevel);
rep.ItemTemplate
=
template;
rep.ItemDataBound
+=
new
RepeaterItemEventHandler(rep_ItemDataBound);
rep.DataSource
=
ds;
rep.DataBind();
item.Controls.Add(rep);
currentLevel
—
;
}
}
void
rep_ItemDataBound(
object
sender, RepeaterItemEventArgs e)
{
DataRowView drv
=
(DataRowView)e.Item.DataItem;
HyperLink link
=
(HyperLink)e.Item.FindControl(
“
innerLink
“
);
if
(link
!=
null
)
{
link.Text
=
drv[
“
categoryname
“
].ToString();
link.NavigateUrl
=
“
../search.aspx?c=
“
+
drv[
“
id
“
].ToString();
ListInnerCategory(e.Item,
int
.Parse(drv[
“
id
“
].ToString()));
}
}
转载于:https://www.cnblogs.com/mcsm/articles/1832653.html
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23756.html