Ok, I think this is an easy one - I've just been staring at it too long.
I'm creating a datagrid in asp.net/c# from scratch, i.e. nothing at all in
the aspx page. I'm also creating all the columns programmatically
(templatecolumns). This part works fine. Next, I am adding ImageButtons to
the headers of the columns, which I am attaching events to and using to sort
the datagrid based on the given column. This works with one major problem -
the datagrid is loaded TWICE. I thought I knew the obvious explanation for
this (I understand postbacks, etc), but I'm obviously missing something.
I tried stepping back and using an example straight from Microsoft's KB. I
created a new asp.net solution using some sample code from the example, and
changed one of the columns in their example to a programmatically created
boundcolumn (it was previously in the aspx page). See code below. If you run
the code below, you'll see the page loads fine, and sorting on the columns
created in the aspx page works fine. However, the programatically created
column simply dissapears when you click it's sort button. What gives??
SortableDataGrid.aspx:
<%@ Page language="c#" Codebehind="SortableDataGrid.aspx.cs"
AutoEventWireup="false" Inherits="dev.SortableDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SortableDataGrid</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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgProducts" style="Z-INDEX: 101; LEFT:
40px; POSITION: absolute; TOP: 32px"
runat="server">
<Columns>
<asp:BoundColumn DataField="UnitPrice"
SortExpression="UnitPrice" HeaderText="UnitPrice"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsInStock"
SortExpression="UnitsInStock" HeaderText="UnitsInStock"></asp:BoundColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>
SortableDataGrid.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace dev
{
/// <summary>
/// Summary description for SortableDataGrid.
/// </summary>
public class SortableDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgProducts;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
BindData("ProductName");
}
private void AddColumns()
{
}
private void BindData(string orderBy)
{
dgProducts.AutoGenerateColumns = false;
dgProducts.AllowSorting = true;
BoundColumn bc1 = new BoundColumn();
bc1.DataField = "ProductName";
bc1.SortExpression = "ProductName";
bc1.HeaderText = "ProductName";
dgProducts.Columns.Add(bc1);
// Connect to the Database
SqlConnection myConnection = new
SqlConnection("server=127.0.0.1;database=Northwind ;uid=TestUser;pwd=TestPwd;");
// Retrieve the SQL query results and bind it to the Repeater
string SQL_QUERY = "SELECT ProductName, UnitPrice,
UnitsInStock " +
"FROM Products ORDER BY " + orderBy;
SqlCommand myCommand = new SqlCommand(SQL_QUERY,
myConnection);
myConnection.Open();
dgProducts.DataSource = myCommand.ExecuteReader();
dgProducts.DataBind();
myConnection.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgProducts.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.dgProducts_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgProducts_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEvent Args e)
{
BindData(e.SortExpression);
}
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.806 / Virus Database: 548 - Release Date: 12/5/2004