more debugs

Sunday, November 27, 2011

Creating Fixed Headers in an ASP.Net GridView using JavaScript

In Reference to  article in CodeProject particular article Grid View With Fixed Header I am stating the problem first and stating the solution.

Problem : Whenever we use grid view along with a webform which include master page. I mean if we place gridview in the ContentPlaceHolder, then the ID of the Grid View is found to be changed
So when we try to pass the ID (GridView1 in above mentioned example) we ge some error due to not finding the id by javascript on runtime.





Solution :
Replace
  ClientScript.RegisterStartupScript(this.GetType(), "CreateGridHeader", "<script>CreateGridHeader('DataDiv', 'GridView1', 'HeaderDiv')</script>");
     
 with
     System.Web.UI.Control cd = Page.Master.FindControl("GridView1");
      if (cd != null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "CreateGridHeader", "<script>CreateGridHeader('DataDiv', 'GridView1', 'HeaderDiv')</script>");
        } 
  // name of the gridview changes on fly when using with master page
        ClientScript.RegisterStartupScript(this.GetType(), "CreateGridHeader", "<script>function PageLoad(){CreateGridHeader('DataDiv', 'ctl00_ContentPlaceHolder1_GridView1', 'HeaderDiv');}</script>");



and add following code to body tag of master page (.aspx)

<body onLoad="PageLoad()">


And get the grid view working as described in code project.



Thank you.

Follow Me