Forum Moderators: open

Message Too Old, No Replies

Button Created at Runtime not Firing Command Method

asp.net / c#

         

Argblat

6:12 pm on Jul 3, 2006 (gmt 0)

10+ Year Member



I have a button that I am creating at runtime and adding to a PlaceHolder. As part of the creation I give it a CommandArgument and a CommandEventHandler

Here is the code to create the button and add it to the PlaceHolder:


Button btnSubmitForPO_Final = new Button();btnSubmitForPO_Final.Text = "View Purchase Order";
btnSubmitForPO_Final.CommandArgument = "It Worked";btnSubmitForPO_Final.Command += new CommandEventHandler(this.btnSubmitForPO_Final_Command);
phOffhireInfo.Controls.Add(btnSubmitForPO_Final);

I then set up a method to catch the ButtonClicks via the CommandArgument


private void btnSubmitForPO_Final_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
Response.Write(e.CommandArgument.ToString());
}

My problem is this. If I write this code on it's own WebForm and test it, it works perfectly fine. However, it is not working within the page that I need it for (which includes a great deal of other code). I have no idea how to test why it isn't working since there really isn't a very good place for me to put a breakpoint (if i put it on the first part of the code it runs through...if i put it on the second part, it never gets there). If i delete the btnSubmitForPO_Final_Command method to catch the Event, compiling throws an error saying it can't find the method, so I know that it's looking for it when it compiles...for whatever reason it doesn't fire on run time

How do I figure out why the CommandEventHandler is not triggering ... I can't think of a proper test case or alternate test to figure out my problem

-Mike

wardbekker

7:35 am on Jul 4, 2006 (gmt 0)

10+ Year Member



Hi Argblat,

Is the button also re-created on postback? Inside the page_load?

If not, the event will not fire correctly.