ASP.NET Web Pages - WebGrid Helper


WebGrid - one of the many useful ASP.NET Web helpers.


Hand-written HTML

In previous chapters, you used Razor code to display database data, and all the HTML markup was hand-written:

Database Example

@{
var db = Database.Open("SmallBakery");
var selectQueryString = "SELECT * FROM Product ORDER BY Name";
}

<html>
<body>
<h1>Small Bakery Products</h1>
<table>
<tr>
<th>Id</th>
<th>Product</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach(var row in db.Query(selectQueryString))
{

<tr>
<td>@row.Id</td>
<td>@row.Name</td>
<td>@row.Description</td>
<td align="right">@row.Price</td>
</tr>
}
</table>
</body>
</html>

Run example »


Using the WebGrid Helper

The WebGrid helper provides a simpler way to display data.

The WebGrid helper:

  • Automatically creates an HTML table to display data
  • Supports different formatting options
  • Supports paginated display of data
  • Supports sorting by clicking the column headers

WebGrid Example

@{
var db = Database.Open("SmallBakery") ;
var selectQueryString = "SELECT * FROM Product ORDER BY Id";
var data = db.Query(selectQueryString);
var grid = new WebGrid(data);
}

<html>
<head>
<title>Displaying Data Using the WebGrid Helper</title>
</head>
<body>
<h1>Small Bakery Products</h1>
<div id="grid">
@grid.GetHtml()
</div>
</body>
</html>

Run example »


Other Extensions