Adding button at runtime: Events not firing

J

John Hughes

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

Karl Seguin

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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top