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:



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) 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 is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

Englestone said...

Useful,

Cheers.

-- Lee

Anonymous said...

Doesn't works.