展开和折叠GridView行

原文地址:http://www.codeproject.com/KB/webforms/GridViewExpandCollapse.aspx
代码下载:/Files/sunfishlu/GridViewExpandCollapse.rar
效果图:
展开和折叠GridView行


介绍
这篇文章介绍了使用JavaScript折叠/展开GridView行的功能,为了实现所希望的效果,我在GridView的表头放置了一张图片,当点击对应图片时,GridView将会折叠和展开行。

HTML代码
在GridView中添加一个模板列,把图片放到模板列中的HeaderTemplate中,此GridView的HTML代码如下:
展开和折叠GridView行展开和折叠GridView行Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1展开和折叠GridView行<asp:GridViewID="gvTab"BackColor="WhiteSmoke"runat="server"AutoGenerateColumns="False"GridLines="Vertical"
2展开和折叠GridView行ShowFooter="True">
3展开和折叠GridView行<Columns>
4展开和折叠GridView行<asp:TemplateField>
5展开和折叠GridView行<HeaderStyleWidth="25px"/>
6展开和折叠GridView行<ItemStyleWidth="25px"BackColor="White"/>
7展开和折叠GridView行<HeaderTemplate>
8展开和折叠GridView行<asp:ImageID="imgTab"onclick="javascript:Toggle(this);"runat="server"ImageUrl="~/minus.gif"
9展开和折叠GridView行ToolTip="Collapse"/>
10展开和折叠GridView行</HeaderTemplate>
11展开和折叠GridView行</asp:TemplateField>
12展开和折叠GridView行<asp:BoundFieldHeaderText="n"DataField="n">
13展开和折叠GridView行<HeaderStyleWidth="25px"/>
14展开和折叠GridView行<ItemStyleWidth="25px"/>
15展开和折叠GridView行</asp:BoundField>
16展开和折叠GridView行<asp:BoundFieldHeaderText="sqrt(n)"DataField="sqrtn">
17展开和折叠GridView行<HeaderStyleWidth="150px"/>
18展开和折叠GridView行<ItemStyleWidth="150px"/>
19展开和折叠GridView行</asp:BoundField>
20展开和折叠GridView行<asp:BoundFieldHeaderText="qbrt(n)"DataField="qbrtn">
21展开和折叠GridView行<HeaderStyleWidth="150px"/>
22展开和折叠GridView行<ItemStyleWidth="150px"/>
23展开和折叠GridView行</asp:BoundField>
24展开和折叠GridView行</Columns>
25展开和折叠GridView行<HeaderStyleHeight="25px"Font-Bold="True"BackColor="DimGray"ForeColor="White"HorizontalAlign="Center"
26展开和折叠GridView行VerticalAlign="Middle"/>
27展开和折叠GridView行<RowStyleHeight="25px"BackColor="Gainsboro"HorizontalAlign="Center"VerticalAlign="Middle"/>
28展开和折叠GridView行<AlternatingRowStyleHeight="25px"BackColor="LightGray"HorizontalAlign="Center"VerticalAlign="Middle"/>
29展开和折叠GridView行<FooterStyleBackColor="Gray"/>
30展开和折叠GridView行</asp:GridView>
31展开和折叠GridView行

GridView的表头图片绑定onclick事件
展开和折叠GridView行展开和折叠GridView行Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->展开和折叠GridView行<asp:ImageID="imgTab"onclick="javascript:Toggle(this);"runat="server"ImageUrl="~/minus.gif"ToolTip="Collapse"/>
展开和折叠GridView行
展开和折叠GridView行

JavaScript代码
把下面的代码放到script标签中。

展开和折叠GridView行展开和折叠GridView行Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->展开和折叠GridView行<scripttype="text/javascript">
展开和折叠GridView行
varGrid=null;
展开和折叠GridView行
varUpperBound=0;
展开和折叠GridView行
varLowerBound=1;
展开和折叠GridView行
varCollapseImage='minus.gif';
展开和折叠GridView行
varExpandImage='plus.gif';
展开和折叠GridView行
varIsExpanded=true;
展开和折叠GridView行
varRows=null;
展开和折叠GridView行
varn=1;
展开和折叠GridView行
varTimeSpan=25;
展开和折叠GridView行
展开和折叠GridView行window.onload
=function()
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行Grid
=document.getElementById('<%=this.gvTab.ClientID%>');
展开和折叠GridView行UpperBound
=parseInt('<%=this.gvTab.Rows.Count%>');
展开和折叠GridView行Rows
=Grid.getElementsByTagName('tr');
展开和折叠GridView行}

展开和折叠GridView行
展开和折叠GridView行
functionToggle(Image)
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行ToggleImage(Image);
展开和折叠GridView行ToggleRows();
展开和折叠GridView行}

展开和折叠GridView行
展开和折叠GridView行
functionToggleImage(Image)
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行
if(IsExpanded)
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行Image.src
=ExpandImage;
展开和折叠GridView行Image.title
='Expand';
展开和折叠GridView行Grid.rules
='none';
展开和折叠GridView行n
=LowerBound;
展开和折叠GridView行
展开和折叠GridView行IsExpanded
=false;
展开和折叠GridView行}

展开和折叠GridView行
else
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行Image.src
=CollapseImage;
展开和折叠GridView行Image.title
='Collapse';
展开和折叠GridView行Grid.rules
='cols';
展开和折叠GridView行n
=UpperBound;
展开和折叠GridView行
展开和折叠GridView行IsExpanded
=true;
展开和折叠GridView行}

展开和折叠GridView行}

展开和折叠GridView行
展开和折叠GridView行
functionToggleRows()
展开和折叠GridView行展开和折叠GridView行
展开和折叠GridView行{
展开和折叠GridView行
if(n<LowerBound||n>UpperBound)return;
展开和折叠GridView行
展开和折叠GridView行Rows[n].style.display
=Rows[n].style.display==''?'none':'';
展开和折叠GridView行
if(IsExpanded)n--;elsen++;
展开和折叠GridView行setTimeout(
"ToggleRows()",TimeSpan);
展开和折叠GridView行}

展开和折叠GridView行
</script>

在上面的代码中,全局参数是在window.onload事件中初始化的。有三个方法:toogle ToogleImage和ToggleRows。Toogle方法响应一个click事件,它先找到表头中的图片,然后通过调用ToogleImage和ToggleRows来实现我们的目的。注意在ToggleRows方法中,为了实现一些动态的效果,我们重复使用了setTimeout方法。
为了在折叠展开中有一定的迟缓,每一次调用ToggleRows方法都设置了25毫秒,你可以根据需要改变TimeSpan的值。

总结
在这篇文章中,我使用了setTimeout来达到流畅展开和折叠的效果。