Click here to Skip to main content
16,020,840 members
Articles / Web Development / ASP.NET
Article

Create a scrollable Gridview in asp.net 2.0

Rate me:
Please Sign up or sign in to vote.
3.14/5 (13 votes)
17 Oct 2006 133.5K   1.2K   45   33
Using Javascript's cloneNode to create scrollable Gridview in asp.net 2.0

Sample Image - ScrollingGridView.gif

Introduction

  Using Javascript's cloneNode ,we can clone a table's header,so we can make a gridview to be scrollable.This method isn't set some long css.

  In asp.net 2.0's Page_Load event,we must add style for GridView, and this MUST for IE and Firefox can work!

C#
protected void Page_Load( object sender, EventArgs e )
{
  if (!IsPostBack)
  {
    GridView1.Attributes.Add("style", "table-layout:fixed");
    GridView1.DataSource = CreateDataSource();
    GridView1.DataBind();
  }
}

 Next,we must add some javascript for it:

JavaScript
function start()
{
 var t = document.getElementById("<%=GridView1.ClientID%>");
 var t2 = t.cloneNode(true)
 for(i = t2.rows.length -1;i > 0;i--)
 t2.deleteRow(i)  
 t.deleteRow(0)  
 a.appendChild(t2) 
}
window.onload = start

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldoesn't work for me Pin
kunalagrawal20-Oct-06 9:07
kunalagrawal20-Oct-06 9:07 
AnswerRe: doesn't work for me Pin
Xianhui Meng20-Oct-06 16:45
Xianhui Meng20-Oct-06 16:45 
GeneralRe: doesn't work for me Pin
kunalagrawal23-Oct-06 7:30
kunalagrawal23-Oct-06 7:30 
GeneralRe: doesn't work for me Pin
Sasha Shipka20-Nov-06 2:05
Sasha Shipka20-Nov-06 2:05 
GeneralRe: doesn't work for me Pin
jason2k28-Oct-06 15:33
jason2k28-Oct-06 15:33 
GeneralRe: doesn't work for me Pin
jason2k28-Oct-06 15:38
jason2k28-Oct-06 15:38 
GeneralRe: doesn't work for me Pin
lovelylive10-Nov-06 18:34
lovelylive10-Nov-06 18:34 
AnswerRe: doesn't work for me Pin
jason2k10-Nov-06 22:49
jason2k10-Nov-06 22:49 
u can't place the code 'window.removed = start' there
The function will be called before the HTML tags are being rendered. Therefore the GridView does not even exist yet during the execution of the function, start. u need to call the function after the GridView tag.

<html>
<head>
<title>Grid View Scrollable</title>
<script language="javascript" type="text/javascript">
function start()
{
var a = document.getElementById("a");
var t = document.getElementById("<%=GridView1.ClientID%>");
var t2 = t.cloneNode(true);
for(i = t2.rows.length -1;i > 0;i--)
t2.deleteRow(i);
t.deleteRow(0);
document.getElementById("a").appendChild(t2);
}
</script>
</head>

<body>
<form>
<asp:GridView id="GridView1">
<Columns>
<!-- Put all your columns here -->
</Columns>
</asp:GridView>

<script language="javascript" type="text/javascript">
start(); //u call the function after the GridView exist.
</script>
</form>
</body>
<html>

Love All, Trust a few, Do harm to none.
GeneralRe: doesn't work for me Pin
lovelylive11-Nov-06 1:09
lovelylive11-Nov-06 1:09 
GeneralRe: doesn't work for me Pin
jason2k11-Nov-06 1:20
jason2k11-Nov-06 1:20 
GeneralRe: doesn't work for me Pin
lovelylive11-Nov-06 2:25
lovelylive11-Nov-06 2:25 
GeneralRe: doesn't work for me Pin
jason2k11-Nov-06 6:36
jason2k11-Nov-06 6:36 
GeneralCan you help me on MAC IE 5.2 [modified] Pin
kindestman24-Sep-08 7:19
kindestman24-Sep-08 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.