This article demonstrates how to create simple UI effects in an ASP.NET GridView control using jQuery. These tips have been tested in IE 7 and Firefox 3.
I assume you are familiar with jQuery. If not, please read my article over here before continuing any further: Using jQuery with ASP.NET - A Beginner's Guide. Update - A new version of jQuery has been released. jQuery 1.3.2 can be downloaded from over here: Download Query 1.3
You can also check out my new EBook on using jQuery with ASP.NET called "51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls"
Set up an ASP.NET GridView as you usually do, binding it to a datasource. For demonstration purposes, here’s some sample markup where I am using the Northwind database and a GridView bound to the SQLDataSource to pull data from the database.
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City] FROM [Customers]">
asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1" AllowPaging="False" AllowSorting="True" >
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
Columns>
asp:GridView>
div>
form>
The element in the web.config will look similar to the following:
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
connectionStrings>
Note: In most of the tips shown here, I am using a complex jQuery row ‘filter’ suggested by Karl Swedberg to a user in a jQuery forum. This filter is required due to the fact that a GridView does not render (accessibility tags) a and a by default(read this article of mine to learn how to make the GridView generate a ). For the header, the GridView generates ’s inside . Similarly for the footer, the GridView generates a inside a
No comments:
Post a Comment