S
Solomon Shaffer
I am trying to add some link buttons to a datagrid template column at
runtime and trying to wire these buttons up to the ItemCommand event (or any
event when clicked!). The ItemCommand event works fine if I add the controls
at design time in the HTML, but does not seem to catch when I add the
control dynamically in the code behind. The datagrid renders just fine, just
no ItemCommand event... I have included some code snipplets below. Any help
you be appreciated.
private void InitializeComponent()
{
this.lnkPageFirst.Click += new
System.EventHandler(this.lnkPageFirst_Click);
this.lnkPagePrev.Click += new System.EventHandler(this.lnkPagePrev_Click);
this.lnkPageNext.Click += new System.EventHandler(this.lnkPageNext_Click);
this.lnkPageLast.Click += new System.EventHandler(this.lnkPageLast_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.grdObjects.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ActionColumn_Clic
k);
}
private void Page_Load(object sender, System.EventArgs e)
{
TemplateColumn oTemplateColumn = new TemplateColumn();
oTemplateColumn.ItemTemplate = new DataGridTemplate( arActionColumnItems );
grdObjects.Columns.AddAt( 0, oTemplateColumn );
}
protected void ActionColumn_Click(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Fire the public usercontrol event
Response.Write( "Got it" );
}
Controls are added to the grid:
public class DataGridTemplate : ITemplate
{
private ArrayList m_arActionColumnItems;
public DataGridTemplate( ArrayList ActionColumnItems )
{
m_arActionColumnItems = ActionColumnItems;
}
public void InstantiateIn( System.Web.UI.Control oContainer )
{
//Add a new link button for each action in the actions array
for ( int i = 0; i < m_arActionColumnItems.Count; i++ )
{
ActionColumnItem oActionColumnItem =
(ActionColumnItem)m_arActionColumnItems;
LinkButton lnkAction = new LinkButton();
lnkAction.Text = oActionColumnItem.ActionText;
lnkAction.CommandArgument = oActionColumnItem.CommandArgument;
lnkAction.CommandName = oActionColumnItem.ActionText;
lnkAction.Attributes.Add("runat","server");
lnkAction.ID = "lnkAction" + i;
oContainer.Controls.Add( lnkAction );
if ( i < m_arActionColumnItems.Count - 1 )
{
oContainer.Controls.Add( new LiteralControl( " | " ) );
}
}
}
}
public class ActionColumnItem
{
protected string sActionText;
protected string sCommandArgument;
public string ActionText
{
get
{
return sActionText;
}
set
{
sActionText = value;
}
}
public string CommandArgument
{
get
{
return sCommandArgument;
}
set
{
sCommandArgument = value;
}
}
public ActionColumnItem( string ActionText, string CommandArgument )
{
sActionText = ActionText;
sCommandArgument = CommandArgument;
}
}
runtime and trying to wire these buttons up to the ItemCommand event (or any
event when clicked!). The ItemCommand event works fine if I add the controls
at design time in the HTML, but does not seem to catch when I add the
control dynamically in the code behind. The datagrid renders just fine, just
no ItemCommand event... I have included some code snipplets below. Any help
you be appreciated.
private void InitializeComponent()
{
this.lnkPageFirst.Click += new
System.EventHandler(this.lnkPageFirst_Click);
this.lnkPagePrev.Click += new System.EventHandler(this.lnkPagePrev_Click);
this.lnkPageNext.Click += new System.EventHandler(this.lnkPageNext_Click);
this.lnkPageLast.Click += new System.EventHandler(this.lnkPageLast_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.grdObjects.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ActionColumn_Clic
k);
}
private void Page_Load(object sender, System.EventArgs e)
{
TemplateColumn oTemplateColumn = new TemplateColumn();
oTemplateColumn.ItemTemplate = new DataGridTemplate( arActionColumnItems );
grdObjects.Columns.AddAt( 0, oTemplateColumn );
}
protected void ActionColumn_Click(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Fire the public usercontrol event
Response.Write( "Got it" );
}
Controls are added to the grid:
public class DataGridTemplate : ITemplate
{
private ArrayList m_arActionColumnItems;
public DataGridTemplate( ArrayList ActionColumnItems )
{
m_arActionColumnItems = ActionColumnItems;
}
public void InstantiateIn( System.Web.UI.Control oContainer )
{
//Add a new link button for each action in the actions array
for ( int i = 0; i < m_arActionColumnItems.Count; i++ )
{
ActionColumnItem oActionColumnItem =
(ActionColumnItem)m_arActionColumnItems;
LinkButton lnkAction = new LinkButton();
lnkAction.Text = oActionColumnItem.ActionText;
lnkAction.CommandArgument = oActionColumnItem.CommandArgument;
lnkAction.CommandName = oActionColumnItem.ActionText;
lnkAction.Attributes.Add("runat","server");
lnkAction.ID = "lnkAction" + i;
oContainer.Controls.Add( lnkAction );
if ( i < m_arActionColumnItems.Count - 1 )
{
oContainer.Controls.Add( new LiteralControl( " | " ) );
}
}
}
}
public class ActionColumnItem
{
protected string sActionText;
protected string sCommandArgument;
public string ActionText
{
get
{
return sActionText;
}
set
{
sActionText = value;
}
}
public string CommandArgument
{
get
{
return sCommandArgument;
}
set
{
sCommandArgument = value;
}
}
public ActionColumnItem( string ActionText, string CommandArgument )
{
sActionText = ActionText;
sCommandArgument = CommandArgument;
}
}