W
Webgour
Hi,
I'm tring to add a column to a datagrid with a linkbutton as header that can
be used to sort the column. The column and the linkbutton are added
programmatically (see below). However the problem is that when you click the
added column header it doesn't trigger the sort.
The code :
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="Admin_Interface03.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ASP
ATAGRID
id="MyDataGrid"
runat="server"
HeaderStyle-ForeColor="black"
HeaderStyle-BackColor="#cccccc"
Font-Size="8pt"
Font-Name="Verdana"
CellPadding="3">
<Columns>
<asp:BoundColumn HeaderText="Sortable" DataField="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundColumn HeaderText="Not Sortable" DataField="BirthDate" />
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton id="SortButton1" runat="server" CommandName="sort"
CommandArgument="LastName">LinkButton</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>' ID="Label1"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP
ATAGRID>
</form>
</body>
</HTML>
code behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Admin_Interface03
{
public class WebForm1 : System.Web.UI.Page
{
private string sDataTableName = "Employees";
private string SortField = "";
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
MyDataGrid.AutoGenerateColumns = false;
MyDataGrid.EnableViewState = true;
MyDataGrid.AllowSorting = true;
MyDataGrid.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(MyDataGrid_Sort);
MyDataGrid.ShowHeader = true;
MyDataGrid.ShowFooter = true;
MyDataGrid.HorizontalAlign = HorizontalAlign.Center;
MyDataGrid.AlternatingItemStyle.BackColor =
System.Drawing.Color.FromName("#F1F1F1");
MyDataGrid.CellPadding = 3;
MyDataGrid.BackColor = System.Drawing.Color.FromName("#FFFFFF");
MyDataGrid.Width = Unit.Pixel(400);
MyDataGrid.GridLines = GridLines.Both;
MyDataGrid.BorderWidth = Unit.Pixel(4);
MyDataGrid.BorderStyle = BorderStyle.Solid;
MyDataGrid.BorderColor = System.Drawing.Color.FromName("#C4C2C2");
MyDataGrid.HeaderStyle.Font.Size = FontUnit.Point(8);
MyDataGrid.HeaderStyle.BackColor =
System.Drawing.Color.FromName("#F3F1F1");
MyDataGrid.ItemStyle.BackColor =
System.Drawing.Color.FromName("#FFFFFF");
if (!IsPostBack)
{
Bind_Grid();
}
}
public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
SortField = (string)e.SortExpression;//OR SortField =
e.SortExpression.ToString();
Response.Write("MyDataGrid_Sort-SortField="+SortField+"<br>");
Bind_Grid();
}
public void Bind_Grid()
{
Response.Write("SortField="+SortField+"<br>");
string sSqlQuery;
if(SortField.Length>0)
{sSqlQuery = "SELECT * FROM "+sDataTableName+" ORDER BY "+SortField;}
else
{sSqlQuery = "SELECT * FROM "+sDataTableName;}
Response.Write("sSqlQuery="+sSqlQuery+"<br>");
System.Data.SqlClient.SqlConnection oConnection = HERE YOU NEED TO CREATE
A CONNECTION TO NORTHWIND
System.Data.SqlClient.SqlCommand oCommand = new
System.Data.SqlClient.SqlCommand(sSqlQuery, oConnection);
System.Data.SqlClient.SqlDataReader oDataReader =
oCommand.ExecuteReader();
System.Web.UI.WebControls.TemplateColumn oTemplateColumn;
oTemplateColumn = new TemplateColumn();
//oTemplateColumn.SortExpression = oDataReader.GetName(0);
oTemplateColumn.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, oDataReader.GetName(0));
oTemplateColumn.ItemTemplate = new DataGridTemplate(ListItemType.Item,
oDataReader.GetName(0));
MyDataGrid.Columns.Add(oTemplateColumn);
MyDataGrid.DataSource = oDataReader;
MyDataGrid.DataBind();
oDataReader.Close();
oConnection.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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Label lb;
switch(templateType)
{
case ListItemType.Header:
LinkButton oLinkButton = new LinkButton();
oLinkButton.Text = "<B>"+columnName+"</B>";
oLinkButton.ID = "SortLink";
oLinkButton.CommandName = "sort";
oLinkButton.CommandArgument = columnName;
// oLinkButton.Command+= new
System.Web.UI.WebControls.CommandEventHandler(OnSortCommand);
container.Controls.Add(oLinkButton); //adds the Control to the Controls
collection of the parent control
break;
case ListItemType.Item:
lb = new Label();
lb.DataBinding += new EventHandler(TemplateControl_DataBinding);
container.Controls.Add(lb);
break;
case ListItemType.Footer:
lb = new Label();
lb.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lb);
break;
}
}
private void TemplateControl_DataBinding(object sender,System.EventArgs e)
{
DataGridItem container;
Label oLabel;
oLabel = (Label) sender;
container = (DataGridItem) oLabel.NamingContainer;
oLabel.Text += DataBinder.Eval(container.DataItem, columnName);
}
}
}
Any help on this would be great.
Thanks in advance.
I'm tring to add a column to a datagrid with a linkbutton as header that can
be used to sort the column. The column and the linkbutton are added
programmatically (see below). However the problem is that when you click the
added column header it doesn't trigger the sort.
The code :
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="Admin_Interface03.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ASP

id="MyDataGrid"
runat="server"
HeaderStyle-ForeColor="black"
HeaderStyle-BackColor="#cccccc"
Font-Size="8pt"
Font-Name="Verdana"
CellPadding="3">
<Columns>
<asp:BoundColumn HeaderText="Sortable" DataField="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundColumn HeaderText="Not Sortable" DataField="BirthDate" />
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton id="SortButton1" runat="server" CommandName="sort"
CommandArgument="LastName">LinkButton</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>' ID="Label1"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP

</form>
</body>
</HTML>
code behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Admin_Interface03
{
public class WebForm1 : System.Web.UI.Page
{
private string sDataTableName = "Employees";
private string SortField = "";
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
MyDataGrid.AutoGenerateColumns = false;
MyDataGrid.EnableViewState = true;
MyDataGrid.AllowSorting = true;
MyDataGrid.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(MyDataGrid_Sort);
MyDataGrid.ShowHeader = true;
MyDataGrid.ShowFooter = true;
MyDataGrid.HorizontalAlign = HorizontalAlign.Center;
MyDataGrid.AlternatingItemStyle.BackColor =
System.Drawing.Color.FromName("#F1F1F1");
MyDataGrid.CellPadding = 3;
MyDataGrid.BackColor = System.Drawing.Color.FromName("#FFFFFF");
MyDataGrid.Width = Unit.Pixel(400);
MyDataGrid.GridLines = GridLines.Both;
MyDataGrid.BorderWidth = Unit.Pixel(4);
MyDataGrid.BorderStyle = BorderStyle.Solid;
MyDataGrid.BorderColor = System.Drawing.Color.FromName("#C4C2C2");
MyDataGrid.HeaderStyle.Font.Size = FontUnit.Point(8);
MyDataGrid.HeaderStyle.BackColor =
System.Drawing.Color.FromName("#F3F1F1");
MyDataGrid.ItemStyle.BackColor =
System.Drawing.Color.FromName("#FFFFFF");
if (!IsPostBack)
{
Bind_Grid();
}
}
public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
SortField = (string)e.SortExpression;//OR SortField =
e.SortExpression.ToString();
Response.Write("MyDataGrid_Sort-SortField="+SortField+"<br>");
Bind_Grid();
}
public void Bind_Grid()
{
Response.Write("SortField="+SortField+"<br>");
string sSqlQuery;
if(SortField.Length>0)
{sSqlQuery = "SELECT * FROM "+sDataTableName+" ORDER BY "+SortField;}
else
{sSqlQuery = "SELECT * FROM "+sDataTableName;}
Response.Write("sSqlQuery="+sSqlQuery+"<br>");
System.Data.SqlClient.SqlConnection oConnection = HERE YOU NEED TO CREATE
A CONNECTION TO NORTHWIND
System.Data.SqlClient.SqlCommand oCommand = new
System.Data.SqlClient.SqlCommand(sSqlQuery, oConnection);
System.Data.SqlClient.SqlDataReader oDataReader =
oCommand.ExecuteReader();
System.Web.UI.WebControls.TemplateColumn oTemplateColumn;
oTemplateColumn = new TemplateColumn();
//oTemplateColumn.SortExpression = oDataReader.GetName(0);
oTemplateColumn.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, oDataReader.GetName(0));
oTemplateColumn.ItemTemplate = new DataGridTemplate(ListItemType.Item,
oDataReader.GetName(0));
MyDataGrid.Columns.Add(oTemplateColumn);
MyDataGrid.DataSource = oDataReader;
MyDataGrid.DataBind();
oDataReader.Close();
oConnection.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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Label lb;
switch(templateType)
{
case ListItemType.Header:
LinkButton oLinkButton = new LinkButton();
oLinkButton.Text = "<B>"+columnName+"</B>";
oLinkButton.ID = "SortLink";
oLinkButton.CommandName = "sort";
oLinkButton.CommandArgument = columnName;
// oLinkButton.Command+= new
System.Web.UI.WebControls.CommandEventHandler(OnSortCommand);
container.Controls.Add(oLinkButton); //adds the Control to the Controls
collection of the parent control
break;
case ListItemType.Item:
lb = new Label();
lb.DataBinding += new EventHandler(TemplateControl_DataBinding);
container.Controls.Add(lb);
break;
case ListItemType.Footer:
lb = new Label();
lb.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lb);
break;
}
}
private void TemplateControl_DataBinding(object sender,System.EventArgs e)
{
DataGridItem container;
Label oLabel;
oLabel = (Label) sender;
container = (DataGridItem) oLabel.NamingContainer;
oLabel.Text += DataBinder.Eval(container.DataItem, columnName);
}
}
}
Any help on this would be great.
Thanks in advance.