D
debartsa
Hi Everybody,
I have a composite control (CompositeControl) which uses an instance of the
DataGrid (DataCtrl1) as a child control.
The DataGrid is dynamically created in the CreateChildControls() method of
my control and is responsible for rendering a list of web sites. The
DataGrid also generates an "Edit" push button alongside each of the list of
names (which will eventually be used for editing the list of items).
Everything works fine! the DataGrid does the work of rendering the list of
names and the "Edit" button as expected. My stumbling block occurs the
"Edit" button is clicked, for some reason the EditCommand event of the
DataGrid isn't firing? Does it have something to do with the .aspx page
consuming the control and the fact that the button click occurs during
postback?
My code ...
// CompositeControl.cs
//
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;
namespace AMT.Portal.Controls {
/// <summary>
/// My Compoiste control which dynamically creates a DataGrid
(DataCtrl1)
/// </summary>
public class CompositeControl : WebControl, INamingContainer
{
protected System.Web.UI.WebControls.DataGrid DataCtrl1;
protected System.Collections.Hashtable myTeamLinks;
protected override void CreateChildControls() {
Controls.Clear();
DataCtrl1 = new DataGrid();
DataCtrl1.AutoGenerateColumns = false;
BoundColumn col = new BoundColumn();
col.HeaderText = "Key";
col.DataField = "Key";
DataCtrl1.Columns.Add(col);
EditCommandColumn EditCol = new EditCommandColumn();
EditCol.EditText = "Edit";
EditCol.UpdateText = "Update";
EditCol.CancelText = "Cancel";
EditCol.ButtonType = ButtonColumnType.PushButton;
DataCtrl1.Columns.Add(EditCol);
DataCtrl1.DataSource = GetLinks();
DataCtrl1.DataBind();
Controls.Add(DataCtrl1);
this.DataCtrl1.EditCommand += new
DataGridCommandEventHandler(this.myDataGrid_EditCommand);
}
/// <summary>
/// Returns an IEnumerable type for the DataGrid to Bind
/// </summary>
private Hashtable GetLinks() {
myTeamLinks = new Hashtable();
myTeamLinks.Add("Microsoft Site", "http://www.microsoft.com");
myTeamLinks.Add("IBM Site", "http://www.ibm.com");
myTeamLinks.Add("Compaq", "http://www.hp.com");
myTeamLinks.Add("Google Site", "http://www.google.com");
return myTeamLinks;
}
/// <summary>
/// The Event Handler that isn't being called when the Edit button
of the DataGrid (Child Control) is clicked ?
/// </summary>
protected void myDataGrid_EditCommand(object sender,
DataGridCommandEventArgs e)
{
Trace.Write("The Data Grid EditCommand Event was fired");
}
Thanks for any help!
Sam
I have a composite control (CompositeControl) which uses an instance of the
DataGrid (DataCtrl1) as a child control.
The DataGrid is dynamically created in the CreateChildControls() method of
my control and is responsible for rendering a list of web sites. The
DataGrid also generates an "Edit" push button alongside each of the list of
names (which will eventually be used for editing the list of items).
Everything works fine! the DataGrid does the work of rendering the list of
names and the "Edit" button as expected. My stumbling block occurs the
"Edit" button is clicked, for some reason the EditCommand event of the
DataGrid isn't firing? Does it have something to do with the .aspx page
consuming the control and the fact that the button click occurs during
postback?
My code ...
// CompositeControl.cs
//
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;
namespace AMT.Portal.Controls {
/// <summary>
/// My Compoiste control which dynamically creates a DataGrid
(DataCtrl1)
/// </summary>
public class CompositeControl : WebControl, INamingContainer
{
protected System.Web.UI.WebControls.DataGrid DataCtrl1;
protected System.Collections.Hashtable myTeamLinks;
protected override void CreateChildControls() {
Controls.Clear();
DataCtrl1 = new DataGrid();
DataCtrl1.AutoGenerateColumns = false;
BoundColumn col = new BoundColumn();
col.HeaderText = "Key";
col.DataField = "Key";
DataCtrl1.Columns.Add(col);
EditCommandColumn EditCol = new EditCommandColumn();
EditCol.EditText = "Edit";
EditCol.UpdateText = "Update";
EditCol.CancelText = "Cancel";
EditCol.ButtonType = ButtonColumnType.PushButton;
DataCtrl1.Columns.Add(EditCol);
DataCtrl1.DataSource = GetLinks();
DataCtrl1.DataBind();
Controls.Add(DataCtrl1);
this.DataCtrl1.EditCommand += new
DataGridCommandEventHandler(this.myDataGrid_EditCommand);
}
/// <summary>
/// Returns an IEnumerable type for the DataGrid to Bind
/// </summary>
private Hashtable GetLinks() {
myTeamLinks = new Hashtable();
myTeamLinks.Add("Microsoft Site", "http://www.microsoft.com");
myTeamLinks.Add("IBM Site", "http://www.ibm.com");
myTeamLinks.Add("Compaq", "http://www.hp.com");
myTeamLinks.Add("Google Site", "http://www.google.com");
return myTeamLinks;
}
/// <summary>
/// The Event Handler that isn't being called when the Edit button
of the DataGrid (Child Control) is clicked ?
/// </summary>
protected void myDataGrid_EditCommand(object sender,
DataGridCommandEventArgs e)
{
Trace.Write("The Data Grid EditCommand Event was fired");
}
Thanks for any help!
Sam