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

GridView多层嵌套和折叠与展开(修改适合自己使用)

程序员文章站 2024-03-04 18:16:06
insus.net近段时间应朋友的要求,写一个gridview多层嵌套和折叠与展开。这个功能的gridview多层嵌套没有问题,因为已经做了无限次数,但是折叠与展开的功能,...
insus.net近段时间应朋友的要求,写一个gridview多层嵌套和折叠与展开。这个功能的gridview多层嵌套没有问题,因为已经做了无限次数,但是折叠与展开的功能,却花上不少时间(网上找资料),虽找到资料可参考,还是了解明它,并修改适合自己程序使用。效果如下:
GridView多层嵌套和折叠与展开(修改适合自己使用)
站点中多个页面使用,因此insus.net把它写在一个用户自定义控件ascx上。
复制代码 代码如下:

insusmenu.ascx

<%@ control language="vb" autoeventwireup="false" codefile="insusmenu.ascx.vb" inherits="ascxcontrols_insusmenu" %>

<!--折叠与展开的javascript部分-->
<script type="text/javascript" language="javascript">
function showhidde(sid, evt) {
evt = evt || window.event;
var target = evt.target || evt.srcelement;
var objdiv = document.getelementbyid("div" + sid);
objdiv.style.display = objdiv.style.display == "none" ? "block" : "none";
target.title = objdiv.style.display == "none" ? "show" : "hide";
var imgid = 'img' + sid;
document.getelementbyid(imgid).src = objdiv.style.display == "none" ? "image/+.gif" : "image/-.gif";
}
</script>
<!--第一层-->
<asp:gridview id="gridviewyear" runat="server" width="100%" autogeneratecolumns="false"
onrowdatabound="gridviewyear_rowdatabound" showheader="false" borderwidth="0">
<columns>
<asp:templatefield>
<itemstyle borderwidth="0" height="25" />
<itemtemplate>

<!--下在这个绑定eval("year"),是为获取唯一id,如果你的记录有一个主键的话,可以绑定主键的字段名-->
<img id='img<%# eval("year")%>' style="border: 0px;" src="image/+.gif" onclick="showhidde('<%#eval("year")%>',event)" />

<!--下面这个绑定,是第一层显示的文字-->
<%# eval("year") & "年"%>
<div id='div<%# eval("year")%>' style="display: none;">

<!--第二层-->
<asp:gridview id="gridviewmonth" runat="server" onrowdatabound="gridviewmonth_rowdatabound" width="100%" autogeneratecolumns="false" showheader="false" borderwidth="0">
<columns>
<asp:templatefield>
<itemstyle borderwidth="0" height="25" />
<itemtemplate>
<!--这部分的解释,可参考上面。-->
<img id='img<%# eval("month")%>' style="border: 0px; padding-left: 20px;" src="image/+.gif" onclick="showhidde('<%#eval("month")%>',event)" />
<%# eval("month") & "月"%>
<div id='div<%# eval("month")%>' style="display: none;">

<!--第三层-->
<asp:gridview id="gridviewvideofile" runat="server" width="100%" autogeneratecolumns="false"
showheader="false" borderwidth="0">
<columns>
<asp:templatefield>
<itemstyle borderwidth="0" height="25" />
<itemtemplate>
<img style="border: 0px; padding-left: 40px; padding-right:5px; width: 16px; height: 16px; vertical-align: middle;" src="image/m_file.png" /><asp:hyperlink id="hyperlinkplayer" runat="server" navigateurl='<%# "~/videoplayer.aspx?videokey=" & eval("videolibrary_nbr")%>' text='<%# eval("oldfilename").substring(0, eval("oldfilename").lastindexof("."))%>' target="_blank"></asp:hyperlink>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
</div>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
</div>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>

复制代码 代码如下:

insusmenu.ascx.vb

imports system.data
imports insus.net

partial class ascxcontrols_insusmenu
inherits system.web.ui.usercontrol

'宣告实例
dim objvideolibrary as new videolibrary()

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()
'从数据库获取数据,绑定在第一层的gridview
me.gridviewyear.datasource = objvideolibrary.getyear()
me.gridviewyear.databind()
end sub

'数据绑定在第二层的gridview
protected sub gridviewyear_rowdatabound(sender as object, e as gridviewroweventargs)


dim dvr as datarowview = directcast(e.row.dataitem, datarowview)
if e.row.rowtype = datacontrolrowtype.datarow then
if e.row.findcontrol("gridviewmonth") isnot nothing then
dim gv as gridview = directcast(e.row.findcontrol("gridviewmonth"), gridview)
objvideolibrary.year = convertdata.tosmallint(dvr("year"))
gv.datasource = objvideolibrary.getmonthbyyear()
gv.databind()
end if
end if
end sub

'数据绑定在第三层的gridview
protected sub gridviewmonth_rowdatabound(sender as object, e as gridviewroweventargs)
dim dvr as datarowview = directcast(e.row.dataitem, datarowview)
if e.row.rowtype = datacontrolrowtype.datarow then
if e.row.findcontrol("gridviewvideofile") isnot nothing then
dim gv as gridview = directcast(e.row.findcontrol("gridviewvideofile"), gridview)
objvideolibrary.year = convertdata.tosmallint(dvr("year"))
objvideolibrary.month = convertdata.totinyint(dvr("month"))
gv.datasource = objvideolibrary.getbyyearandmonth()
gv.databind()
end if
end if
end sub
end class