Класс :
Код
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class SortingDataGrid1 : Page
{
protected DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//On the first request, Bind the DataGrid
BindData(null);
}
}
private void BindData(string _sortExpression)
{
//Create the DataView and cache it to improvwe performance
//BooksData _booksData = new BooksData();
BooksData _booksData = new BooksData();
DataView _data = _booksData.GetAllBooks();
//If the _sortExpression has a value, sort the
//DataView, otherwise just bind the DataGrid
if(_sortExpression != null)
{
_data.Sort = _sortExpression;
}
//Bind the DataGrid
DataGrid1.DataSource = _data;
DataGrid1.DataBind();
}
protected void SortCommand_OnClick(object sender, DataGridSortCommandEventArgs e)
{
//When a sort link is clicked, bind the
//DataGrid using the SortExpression
BindData(e.SortExpression);
}
}
|
ASP страница :
[code]
<%@ Page language='c#' Codebehind='SortingDataGrid1.aspx.cs' Inherits='DayOfDotNet.SortingDataGrid1' %>
<html>
<body style='font: x-small Verdana, Arial, sans-serif;'>
<!-- Begin Web Form -->
<form id='SortingDataGrid1' method='post' runat='server'>
<p>
<a href='/ADayOfDotNet/'>Parent Directory</a>
</p>
<!-- Begin DataGrid -->
<asp

ataGrid id='DataGrid1' runat='server' AutogenerateColumns='False' CellPadding='2' CellSpacing='0' Font-Names='Verdana, Arial, sans-serif' BorderColor='Black' BorderWidth='1' GridLines='Horizontal' AllowSorting='True' OnSortCommand='SortCommand_OnClick'>
<HeaderStyle Font-Bold='True' Font-Size='small' Font-Name='Arial' BackColor='Maroon' ForeColor='White' />
<ItemStyle Font-Size='x-small' />
<AlternatingItemStyle BackColor='Tan' />
<Columns>
<asp:TemplateColumn ItemStyle-VerticalAlign='Top'>
<ItemTemplate>
<img src='images/<%# DataBinder.Eval(Container.DataItem, 'ImageSrc') %>' Alt='<%# DataBinder.Eval(Container.DataItem, 'Title' ) %>'>
</ItemTemplate>
</asp:TemplateColumn>
<asp:HyperLinkColumn DataTextField='Title' DataNavigateUrlField='ISBN' DataNavigateUrlFormatString='DynamicEmbeddedDataGr id.aspx?isbn={0}' HeaderText='Title' ItemStyle-VerticalAlign='Top' SortExpression='Title' />
<asp:TemplateColumn ItemStyle-VerticalAlign='Top' HeaderText='Info (Sort by Price)' SortExpression='Price'>
<ItemTemplate>
<b>Publisher:</b> <a href='<%# DataBinder.Eval(Container.DataItem, 'PublisherURL') %>' target='_blank'>
<%# DataBinder.Eval(Container.DataItem, 'PublisherName') %>
</a>
<b>Release Date:</b>
<%# DataBinder.Eval(Container.DataItem, 'ReleaseDate', '{0:d}') %>
<b>Price:</b>
<%# DataBinder.Eval(Container.DataItem, 'Price', '{0:c}') %>
<a href='<%# DataBinder.Eva