jQuery and ASP.NET

January 2, 2009

Create an ASP.NET GridView Paging Style programmatically




Some time back on dotnetcurry.com, I had posted an article about Some Tips and Tricks while Using ASP.NET GridView Paging. Most of the tips shown were done using CSS + code.

However, if you have a requirement to create a GridView paging style programmatically, then use the OnRowDataBound event of the GridView as shown below:

C#


protected void GridView1_RowDataBound(object sender, 
GridViewRowEventArgs e)

{

  if (e.Row.RowType == DataControlRowType.Pager)

  {

      TableRow tRow = e.Row.Controls[0].Controls[0].
        Controls[0] as TableRow;

      foreach (TableCell tCell in tRow.Cells)

      {

          Control ctrl = tCell.Controls[0];               

          if (ctrl is LinkButton)

          {

              LinkButton lb = (LinkButton)ctrl;

              lb.Width = Unit.Pixel(15);

              lb.BackColor = System.Drawing.Color.DarkGray;

              lb.ForeColor = System.Drawing.Color.White;

              lb.Attributes.Add("onmouseover", 

                 "this.style.backgroundColor='#4f6b72';"); 

              lb.Attributes.Add("onmouseout", 

                "this.style.backgroundColor='darkgray';");

          }

      }

  }
}


VB.NET


Protected Sub GridView1_RowDataBound(ByVal sender As Object, _

                             ByVal e As GridViewRowEventArgs)

     If e.Row.RowType = DataControlRowType.Pager Then

         Dim tRow As TableRow = _

         TryCast(e.Row.Controls(0).Controls(0).Controls(0), _
                                              TableRow)

         For Each tCell As TableCell In tRow.Cells

             Dim ctrl As Control = tCell.Controls(0)

             If TypeOf ctrl Is LinkButton Then

                 Dim lb As LinkButton = CType(ctrl, LinkButton)

                 lb.Width = Unit.Pixel(15)

                 lb.BackColor = System.Drawing.Color.DarkGray

                 lb.ForeColor = System.Drawing.Color.White

                 lb.Attributes.Add("onmouseover", _

                    "this.style.backgroundColor='#4f6b72';")

                 lb.Attributes.Add("onmouseout", _

                    "this.style.backgroundColor='darkgray';")

             End If

         Next tCell

     End If

End Sub



I have set the mouseover and mouseout attributes in this example which changes the color when the user hovers over the pager. You could follow a similar technique or tweak the example to suit your requirement. I hope you get the idea.

The output would be similar to the one shown below:



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Create an ASP.NET GridView Paging Style programmatically"
  1. Englestone said...
    September 2, 2009 3:49 AM

    Useful,

    Cheers.

    -- Lee

  2. Anonymous said...
    August 10, 2010 2:27 AM

    Doesn't works.

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions