K
Ken Tucker
I've read about this issue in many articles across the net... But haven't
found a solution. I see all kinds of custom code to perform sorting with
datagrids, but my example is so simple, I must just be missing something.
Basically, I have a .aspx page that is very simple and has a simple
datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all
datagrid work programmatically). The datagrid is populated from a SQL table
and prints just fine in the browser. I can click on any of the headers (for
sorting) but it appears that once clicking, the sortgrid function isn't
firing and I'm getting no results (empty page mostly) back.
I **HAVE** gotten this to work when I move all the datagrid "code" into the
aspx page, but since this is a template I'm using across many pages, I want
to put it all in the code-behind and make it totally dynamic on the calling
page... I refuse to give up and move away from the code-behind.
I have several debug areas in here, one of which just does response.writes,
and another that changes a label so I know what has fired. I never ever get
anything from SortGrid (both response.write and label) to show in the
resulting HTML, so it appears to never get called when a column is clicked
on (resulting from SortCommand, see below).
The problem must lie in this line:
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
because that's the spot where it tells what to do with the event... But I
can't figure it out for the life of me.
Thanks for any help you can provide.... Links, etc!
-Ken
Here's the code.
****************
kbresults.aspx:
****************
<%@ Page language="c#" Src="kbresults.aspx.cs" Inherits="test.kb"
enableViewState="True" AutoEventWireup="True"%>
<html>
<head></head>
<body>
<form runat="server" id=form1 name=form1>
<aspataGrid id="myDataGrid" runat="server"/>
</form>
<asp:Label id=testola Text="Default" runat=server/>
</body>
</html>
****************
kbresults.aspx.cs
****************
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace test
{
public class kb : System.Web.UI.Page
{
public DataGrid myDataGrid;
protected Label testola;
public DataSet ds;
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid("");
if (IsPostBack)
{
Response.Write ("PostBack");
}
}
public void BindGrid(string sortField)
{
//debug text
Response.Write ("BindGrid");
//debug label
testola.Text = "BindGrid";
SqlConnection dsConn = new SqlConnection("blah blah");
dsConn.Open();
SqlDataAdapter dsAdapter = new SqlDataAdapter("SELECT blah from blah'",
dsConn);
DataSet ds = new DataSet();
dsAdapter.Fill(ds, "TABLE");
DataView source = new DataView(ds.Tables["TABLE"]);
//source.Sort = sortField;
myDataGrid.DataSource = source;
myDataGrid.AutoGenerateColumns = false;
myDataGrid.AllowSorting = true;
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
// Add columns
BoundColumn col1 = new BoundColumn();
col1.DataField = "Category";
col1.SortExpression = "Category";
col1.HeaderText = "Category";
myDataGrid.Columns.Add(col1);
BoundColumn col2 = new BoundColumn();
col2.DataField = "ID";
col2.SortExpression = "ID";
col2.HeaderText = "ID";
myDataGrid.Columns.Add(col2);
HyperLinkColumn col3 = new HyperLinkColumn();
col3.DataNavigateUrlField = "ID";
col3.DataNavigateUrlFormatString = "blah.asp";
col3.DataTextField = "Title";
col3.SortExpression = "Title";
col3.HeaderText = "Title";
myDataGrid.Columns.Add(col3);
BoundColumn col4 = new BoundColumn();
col4.DataField = "DateUpdated";
col4.SortExpression = "DateUpdated";
col4.HeaderText = "Date";
myDataGrid.Columns.Add(col4);
BoundColumn col5 = new BoundColumn();
col5.DataField = "EnteredBy";
col5.SortExpression = "EnteredBy";
col5.HeaderText = "Entered By";
myDataGrid.Columns.Add(col5);
myDataGrid.DataBind();
}
protected void SortGrid(Object src, DataGridSortCommandEventArgs e)
{
//debug label
testola.Text = "You sorted";
//BindGrid((string)e.SortExpression);
//debug text
Response.Write ("SortGrid");
}
}
}
found a solution. I see all kinds of custom code to perform sorting with
datagrids, but my example is so simple, I must just be missing something.
Basically, I have a .aspx page that is very simple and has a simple
datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all
datagrid work programmatically). The datagrid is populated from a SQL table
and prints just fine in the browser. I can click on any of the headers (for
sorting) but it appears that once clicking, the sortgrid function isn't
firing and I'm getting no results (empty page mostly) back.
I **HAVE** gotten this to work when I move all the datagrid "code" into the
aspx page, but since this is a template I'm using across many pages, I want
to put it all in the code-behind and make it totally dynamic on the calling
page... I refuse to give up and move away from the code-behind.
I have several debug areas in here, one of which just does response.writes,
and another that changes a label so I know what has fired. I never ever get
anything from SortGrid (both response.write and label) to show in the
resulting HTML, so it appears to never get called when a column is clicked
on (resulting from SortCommand, see below).
The problem must lie in this line:
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
because that's the spot where it tells what to do with the event... But I
can't figure it out for the life of me.
Thanks for any help you can provide.... Links, etc!
-Ken
Here's the code.
****************
kbresults.aspx:
****************
<%@ Page language="c#" Src="kbresults.aspx.cs" Inherits="test.kb"
enableViewState="True" AutoEventWireup="True"%>
<html>
<head></head>
<body>
<form runat="server" id=form1 name=form1>
<aspataGrid id="myDataGrid" runat="server"/>
</form>
<asp:Label id=testola Text="Default" runat=server/>
</body>
</html>
****************
kbresults.aspx.cs
****************
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace test
{
public class kb : System.Web.UI.Page
{
public DataGrid myDataGrid;
protected Label testola;
public DataSet ds;
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid("");
if (IsPostBack)
{
Response.Write ("PostBack");
}
}
public void BindGrid(string sortField)
{
//debug text
Response.Write ("BindGrid");
//debug label
testola.Text = "BindGrid";
SqlConnection dsConn = new SqlConnection("blah blah");
dsConn.Open();
SqlDataAdapter dsAdapter = new SqlDataAdapter("SELECT blah from blah'",
dsConn);
DataSet ds = new DataSet();
dsAdapter.Fill(ds, "TABLE");
DataView source = new DataView(ds.Tables["TABLE"]);
//source.Sort = sortField;
myDataGrid.DataSource = source;
myDataGrid.AutoGenerateColumns = false;
myDataGrid.AllowSorting = true;
myDataGrid.SortCommand += new DataGridSortCommandEventHandler(SortGrid);
// Add columns
BoundColumn col1 = new BoundColumn();
col1.DataField = "Category";
col1.SortExpression = "Category";
col1.HeaderText = "Category";
myDataGrid.Columns.Add(col1);
BoundColumn col2 = new BoundColumn();
col2.DataField = "ID";
col2.SortExpression = "ID";
col2.HeaderText = "ID";
myDataGrid.Columns.Add(col2);
HyperLinkColumn col3 = new HyperLinkColumn();
col3.DataNavigateUrlField = "ID";
col3.DataNavigateUrlFormatString = "blah.asp";
col3.DataTextField = "Title";
col3.SortExpression = "Title";
col3.HeaderText = "Title";
myDataGrid.Columns.Add(col3);
BoundColumn col4 = new BoundColumn();
col4.DataField = "DateUpdated";
col4.SortExpression = "DateUpdated";
col4.HeaderText = "Date";
myDataGrid.Columns.Add(col4);
BoundColumn col5 = new BoundColumn();
col5.DataField = "EnteredBy";
col5.SortExpression = "EnteredBy";
col5.HeaderText = "Entered By";
myDataGrid.Columns.Add(col5);
myDataGrid.DataBind();
}
protected void SortGrid(Object src, DataGridSortCommandEventArgs e)
{
//debug label
testola.Text = "You sorted";
//BindGrid((string)e.SortExpression);
//debug text
Response.Write ("SortGrid");
}
}
}