PC Review


Reply
Thread Tools Rate Thread

Adding button at runtime: Events not firing

 
 
John Hughes
Guest
Posts: n/a
 
      11th Aug 2005
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.OpenPriceList_Click);
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(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);
}


 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      11th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.OpenPriceList_Click);
> this.DataGrid1.ItemCommand += new
> System.Web.UI.WebControls.DataGridCommandEventHandler(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);
> }
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding events in app during runtime? buu Microsoft Dot NET 1 4th Jul 2008 12:19 AM
dynamically adding controls with events (but events are not firing) SevDer Microsoft ASP .NET 2 13th Nov 2007 06:33 AM
UserControl Events Not Firing with Dynamically Created Button Control jasinx@gmail.com Microsoft ASP .NET 2 6th May 2007 03:12 AM
Button Click Events not firing .NET 2.0 =?Utf-8?B?Vmk=?= Microsoft ASP .NET 1 29th Jan 2006 02:58 AM
Com Addin button not firing click events Phil Microsoft Outlook Program Addins 1 11th Aug 2005 09:55 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:26 AM.