Hi Tim,
Thanks for your followup. From the code you provided, I've got that the
problem is caused by the columns that be added dynamically into the
datagrid according to the columns in the datatable you retrieved from
database. In ASP.NET webform, if we need to dynamically add control onto
it, we need to add it every time the page is request( every time in the
Page_Load or Page_Init). If we only add it when (!IsPostBack), then when
the page is posted back(hit some submit button) , the dynamically added
control will disappear. For example, if we add the following code in
Page_Load to add a textbox in a parent container control:
if(!IsPostBack)
{
TextBox txtDynamic = new TextBox();
txtDynamic.ID = "txtDynamic";
this.Controls.Add(txtDynamic);
}
Then, if the page is postedback, the textbox will disappear.
So the problem in our issue is also caused by this, because you add the
columns dynamically in the
if(!IsPostBack)
{
..
}
block , then when we hit the "sort link" to fire the datagrid's
SortCommand( page is posted back), the columns disappeared, and apparently
the SortCommand won't be fired( because the event mapping has been lost).
Here are two means to resolve it:
1. To initally add enough boundcolumns at design time and set their visible
as "false". Then, at runtime set the proper columns' visible as true and
modify their attrbutes.
2. Still add the columns add runtime, but need to add them every time page
is requested(no matter postback or not). By this means, you need to store
the columns' information need to readded in a proper place(session, or a
hidden field).
Here is a demo page which including the both means above, please check it
out and if you have anything unclear, please feel free to post here.
====================aspx page ====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SortColumnGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>This Grid Add Columns Dynamicaly:<br>
<asp:datagrid id="dgDynamic" runat="server" AllowSorting="True"
AutoGenerateColumns="False"></asp:datagrid><INPUT id="hdColumns"
type="hidden" runat="server"></td>
</tr>
<tr>
<td>This Grid Add Columns Staticaly<br>
<asp:datagrid id="dgStatic" runat="server" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn Visible="False" HeaderText="c1"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c2"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c3"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c4"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c5"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c6"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c7"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c8"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c9"></asp:BoundColumn>
<asp:BoundColumn Visible="False" HeaderText="c10"></asp:BoundColumn>
</Columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</HTML>
=================code behind page class============
public class SortColumnGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgDynamic;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdColumns;
protected System.Web.UI.WebControls.DataGrid dgStatic;
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void BindStaticColumns()
{
DataTable tb = GetDataSource();
int icol = 0;
foreach(System.Data.DataColumn dc in tb.Columns)
{
BoundColumn bc = (BoundColumn)dgStatic.Columns[icol];
bc.DataField = dc.ColumnName;
bc.HeaderText = dc.ColumnName;
bc.SortExpression = dc.ColumnName + " ASC";
bc.Visible = true;
dgStatic.Columns[icol].Visible = true;
icol++;
}
dgStatic.DataSource = tb;
dgStatic.DataBind();
}
protected void BindDynamicColumns()
{
DataTable tb = GetDataSource();
string[] cols = new string[tb.Columns.Count];
int icol = 0;
foreach(System.Data.DataColumn dc in tb.Columns)
{
System.Web.UI.WebControls.BoundColumn bc = new BoundColumn();
bc.DataField = dc.ColumnName;
bc.HeaderText = dc.ColumnName;
bc.SortExpression = dc.ColumnName + " ASC";
bc.Visible = true;
dgDynamic.Columns.Add(bc);
cols[icol] = dc.ColumnName;
icol++;
}
dgDynamic.DataSource = tb;
dgDynamic.DataBind();
hdColumns.Value = string.Join("#",cols);
}
protected void ReSetupDynamicColumns()
{
char[] flags = {'#'};
string hdColumns = Request.Form["hdColumns"];
string[] cols = hdColumns.Split(flags);
for(int i=0;i<cols.Length;i++)
{
System.Web.UI.WebControls.BoundColumn bc = new BoundColumn();
bc.DataField = cols
;
bc.HeaderText = cols;
bc.SortExpression = cols + " ASC";
bc.Visible = true;
dgDynamic.Columns.Add(bc);
}
}
protected DataTable GetDataSource()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("price",typeof(double));
DataRow dr = null;
bool[] flags = {true,false};
for(int i=1;i<=4;++i)
{
dr = tb.NewRow();
dr["index"] = i.ToString();
dr["name"] = "Name" + i.ToString();
dr["price"] = 3.434 * (i%3 +1);
tb.Rows.Add(dr);
}
return tb;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
if(!IsPostBack)
{
BindStaticColumns();
BindDynamicColumns();
}
else
{
ReSetupDynamicColumns();
}
}
private void InitializeComponent()
{
this.dgDynamic.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgDynamic_Sor
tCommand);
this.dgStatic.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgStatic_Sort
Command);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgStatic_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
Response.Write("<br>dgStatic's SortCommand is fired at: " +
DateTime.Now.ToLongTimeString());
Response.Write("<br>SortExpression: " + e.SortExpression );
}
private void dgDynamic_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
Response.Write("<br>dgDynamic's SortCommand is fired at: " +
DateTime.Now.ToLongTimeString());
Response.Write("<br>SortExpression: " + e.SortExpression );
}
}
============================================================
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx