Hi all,
For all you C# coders out there, I've included the article's code in C# to save anyone the headache of converting it.
Code:
<%@ Page Language="C#" Debug="False" Strict="True" Explicit="True" Buffer="True"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Dynamic Column Sorting and Paging in ASP.NET</title>
</head>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0">
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e) {
if(SortExp.Text == ""){
BindSQL ("title asc");
} else {
BindSQL (SortExp.Text);
}
}
void BindSQL (string SortField) {
SqlConnection MyConnection;
SqlDataAdapter MyCommand;
string sqlStr = "SELECT authors.au_fname, authors.au_lname, titles.title, titles.price FROM authors, titleauthor, titles WHERE titleauthor.au_id = authors.au_id AND titleauthor.title_id = titles.title_id ORDER BY title asc";
string strConn = "server=(local);uid=sa;pwd=;database=Pubs;";
int RcdCount;
SortExp.Text = SortField;
MyConnection = new SqlConnection(strConn);
MyCommand = new SqlDataAdapter(sqlStr, MyConnection);
DataSet DS = new DataSet();
MyCommand.Fill(DS, "Pubs");
DataView Source = DS.Tables[0].DefaultView;
Source.Sort = SortField;
Pubs.DataSource = Source;
Pubs.DataBind();
}
string SortOrder (string Field) {
string so = SortExp.Text;
string newString;
if (Field == so) {
newString = Field.Replace ("asc","desc");
}else {
newString = Field.Replace ("desc","asc");
}
return newString;
}
public void BookList_Sort (Object Sender, DataGridSortCommandEventArgs E) {
Pubs.CurrentPageIndex = 0; //To sort from top
BindSQL (SortOrder (E.SortExpression).ToString()); //Rebind our Datagrid
}
public void BookList_PageChange (Object Source, DataGridPageChangedEventArgs E) {
Pubs.CurrentPageIndex = E.NewPageIndex;
BindSQL(SortExp.Text);
}
</script>
<BR><BR>
<H2>Dynamic Column Sorting and Paging in ASP.NET</H2>
<BR><BR>
<form runat="server">
<ASP:Datagrid id="Pubs" runat="server"
Pagesize="10"
AllowSorting="True"
AllowPaging="True"
AllowCustomPaging="False"
PagerStyle-Visible = "True"
PagerStyle-Mode = "NumericPages"
HeaderStyle-BackColor="Blue"
HeaderStyle-ForeColor="White"
OnSortCommand="BookList_Sort"
OnPageIndexChanged="BookList_PageChange"
AutoGenerateColumns="false"
>
<Columns>
<asp:BoundColumn HeaderText="Title" SortExpression="title asc" DataField="title" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="Last Name" SortExpression="au_lname asc" DataField="au_lname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="First Name" SortExpression="au_fname asc" DataField="au_fname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="Price" SortExpression="price asc" DataField="price" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
</Columns>
</ASP:DataGrid>
<asp:Label id="SortExp" runat="server" Visible="False" />
</form>
</body>
</html>
- Jimmy Markatos