How To add Vertical Scroll in GridView and maintain Div's Scroll Position

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewPostion.aspx.cs" Inherits="GridViewPostion" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
  <style type="text/css">
     #divTest{width:500px;height:200px;overflow:auto}
   </style>
   <script type="text/javascript">
     window.onload = function(){
       var strCook = document.cookie;
       if(strCook.indexOf("!~")!=0){
         var intS = strCook.indexOf("!~");
         var intE = strCook.indexOf("~!");
         var strPos = strCook.substring(intS+2,intE);
         document.getElementById("divTest").scrollTop = strPos;
       }
     }
     function SetDivPosition(){
       var intY = document.getElementById("divTest").scrollTop;
       document.title = intY;
       document.cookie = "yPos=!~" + intY + "~!";
     }
   </script>
</head>
<body>
   <form id="form1" runat="server">
   <div id="divTest" onscroll="SetDivPosition()">
  <asp:GridView ID="GridView1" runat="server"></asp:GridView>
 
   </div>
   </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewPostion : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           if (Session["strTemp"] != null)
           {

               GridView1.DataSource = Session["strTemp"] as DataTable;

               GridView1.DataBind();


           }
           else
           {
               GridView1.DataSource = GetCustomMadeDataTable();


               GridView1.DataBind();
           }
       }

   }
   public DataTable GetCustomMadeDataTable()
   {

       //Create a new DataTable object

       System.Data.DataTable objDataTable = new System.Data.DataTable();

       //Create three columns with string as their type

       objDataTable.Columns.Add("Id", typeof(string));
       objDataTable.Columns.Add("Column1", typeof(string));
       objDataTable.Columns.Add("Column2", typeof(string));
       objDataTable.Columns.Add("Column3", typeof(string));


       //Adding some data in the rows of this DataTable
       DataRow dr;
       for (int i = 0; i <= 20; i++)
       {
           dr = objDataTable.NewRow();
           dr[0] = i.ToString();
           dr[1] = "Column1Data" + i.ToString();
           dr[2] = "Column2Data" + i.ToString();
           dr[3] = "Column3Data" + i.ToString();
           objDataTable.Rows.Add(dr);
       }
       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["strTemp"] = objDataTable; return objDataTable;
   }
}

Reactions

Post a Comment

0 Comments

Close Menu