473,503 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding button at runtime: Events not firing

Im adding a button at runtime to a datagrid but the DataGrid1.ItemCommand
event does not
fire. I also have a button that I added at design time and this button does
fire the event ItemCommand.

Any ideas?

My Code :

private void InitializeComponent()
{
this.OpenPriceList.Click += new
System.Web.UI.ImageClickEventHandler(this.OpenPric eList_Click);
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);

}

private void OpenPriceList_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//Button on form which enables grid

ButtonColumn myColumn8 = new ButtonColumn();
myColumn8.Text="<IMG src=images/Info.gif border=0>";
myColumn8.ButtonType=ButtonColumnType.LinkButton;
myColumn8.CommandName="Select";
DataGrid1.Columns.Add(myColumn8);

DataGrid1.DataBind();
DataGrid1.Visible=true;
}

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text);
}
Nov 19 '05 #1
1 1747
The dynamically added button has to be re-added on postback during or before
the onLoad event.

What you likely have now is:

Create button "X"
User clicks button "X"
Page postbacks/reload
Page framework looks for button "X" but doesn't find it

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"John Hughes" <jh******@msn.com> wrote in message
news:u8**************@TK2MSFTNGP09.phx.gbl...
Im adding a button at runtime to a datagrid but the DataGrid1.ItemCommand
event does not
fire. I also have a button that I added at design time and this button
does
fire the event ItemCommand.

Any ideas?

My Code :

private void InitializeComponent()
{
this.OpenPriceList.Click += new
System.Web.UI.ImageClickEventHandler(this.OpenPric eList_Click);
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);

}

private void OpenPriceList_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//Button on form which enables grid

ButtonColumn myColumn8 = new ButtonColumn();
myColumn8.Text="<IMG src=images/Info.gif border=0>";
myColumn8.ButtonType=ButtonColumnType.LinkButton;
myColumn8.CommandName="Select";
DataGrid1.Columns.Add(myColumn8);

DataGrid1.DataBind();
DataGrid1.Visible=true;
}

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text);
}

Nov 19 '05 #2

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

Similar topics

14
5422
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
2
1794
by: Doug Slocum | last post by:
Hi, I'm using ASP.NET. I need help adding new buttons to my web page at run time on the server side prior to sending to the client. As the web form is loading, I won't know in advance how many...
5
11681
by: Ben Fidge | last post by:
I've got a problem where some buttons placed on a user control are only firing their OnClick events when the user clicks on them for the second time. I've got the situation where some common...
0
5709
by: Oz | last post by:
Hi Using VS.NET 2003, Windows XP SP1, We have a page which has been developed using ASP.NET. On it, is a button which when clicked is supposed to add some data to a table. When the button is...
2
9834
by: ~~~ .NET Ed ~~~ | last post by:
I have a problem (don't we all?). I have a web form with multiple modules, some of these modules have an ASP.NET (server run) button. OK, now I have UserControlX which has one such button (say...
0
1424
by: Klaus Jensen | last post by:
Hi! This has been annoying me for a while now, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and...
1
3123
by: Vi | last post by:
Hi, I have an ASP.NET web app and for some reason the Button Click Events are not firing. I ran C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -c and it created the aspnet_client...
6
1584
by: Totto | last post by:
Hi, Anyone know the best solution to dynamically add buttons to a asp 2.0 page using data from Sql server? Are there any contols suitable for this or is it best to iterate the dataset and...
5
5853
by: Tony | last post by:
I am continuing to develop an Access 2007 application which was originally converted from Access 2003. In Access 2003 I was able to disable the Access Close button in the top righthand corner of...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
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
7323
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
6984
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
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
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,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
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
1507
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.