473,473 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ItemCommand in DataGrid control

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.DataGridCommandEventHand ler(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[i];
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( "&nbsp;|&nbsp;" ) );
}
}
}
}

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;
}
}

Nov 18 '05 #1
0 1266

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Li Zhang | last post by:
I have a user control contain one datagrid. this datagrid contains one button column with commandName "Review", I have event handler datagrid_ItemCommand to handle this event and also wired the...
2
by: Gilles T. | last post by:
I have on the same page this code: <script language="VB" runat="server"> ... ... Private Sub subDatagrid_ItemCommand(ByVal source As Object, ByVal e As...
0
by: Mark Kelly | last post by:
(originally posted on framework.aspnet.datagridcontrol) Hi, I don't seem to be able to get a imagebutton (within a template column) to fire an itemcommand event when clicked. Looking through this...
2
by: Matthew | last post by:
I have a DataGrid nested within a DataList. The DataGrid generates LinkButtons with specific CommandName values. I cannot figure out how to trap this event. I need to know which DataGrid...
2
by: Syed Sami R. Shah | last post by:
hi all, i am having problem with using itemcommand of datagrid by way of asp:image button. i have put an image button in a datagrid item template and have set CommandName="DeleteRecord" and have...
2
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file...
4
by: EvelynAnd Ethan | last post by:
Hi, ItemCommand event not firing from a dynamic user control ,WHERE A DATAGRID HAS BUTTON,when i click on the linkbutton first time the itemcommand event doesnt fire,second time event fires up ...
3
by: danc | last post by:
I have a datagrid with a checkbox and dropdown list in each row. Both set AutoPostBack to true and ItemCommand and OnSelectedIndexChanged events for these controls works fine when DataGrid is not...
0
by: jigsawcube1 | last post by:
I am trying to accept two input strings and concatinate and display the result in a datagrid. Following is the HTML and code. ItemCommand does not get fired what is that I am missing? <body...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.