Event not getting thrown C#

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Code:
        Button releaseNumber = new Button();
        releaseNumber.CssClass = tempRow.CssClass;
        releaseNumber.Text = connection.DataReader["Release_Number"].ToString();
        releaseNumber.BorderWidth = 0;
        releaseNumber.ForeColor = System.Drawing.Color.Blue;
        releaseNumber.Click += new EventHandler(SelectReleaseFromTable);
        tempRow.Cells[0].Controls.Add(releaseNumber);

Event doesn't get thrown when I click the button. I can comment out the add event line and the program acts the same way.
 
Last edited:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Are you using WPF? Try using a routedEventHandler like
Code:
releaseNumber.Click += new RoutedEventHandler(SelectReleaseFromTable);
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Are you using WPF? Try using a routedEventHandler like
Code:
releaseNumber.Click += new RoutedEventHandler(SelectReleaseFromTable);

It's for a webpage, and I'm not too familiar with WPF (just googled it now). I'll give that a try when I get back to work tomorrow (no access from home).
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Some more context from the page would be useful. If you're manually wiring up events you have to be careful about when/where you do that. First, you have to wire it up every time; you can't skip it on postback. Second, you have to wire it up before the client-side events are processed on the server, meaning that page_load is too late, I believe. You also have to give the button an ID, as joshsquall mentioned, because that's how the pipeline figures out what event a client-side action maps to on the server. Anyway, it's very likely one of these things, so post some more of the page and we'll take a look.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Some more context from the page would be useful. If you're manually wiring up events you have to be careful about when/where you do that. First, you have to wire it up every time; you can't skip it on postback. Second, you have to wire it up before the client-side events are processed on the server, meaning that page_load is too late, I believe. You also have to give the button an ID, as joshsquall mentioned, because that's how the pipeline figures out what event a client-side action maps to on the server. Anyway, it's very likely one of these things, so post some more of the page and we'll take a look.

Kk and thanks. I'll give that stuff a try and try to post more of it tomorrow. I don't have access to the source code at home.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
So, the button I'm trying to make doesn't exist when the page is first loaded. It's in a table I populated with info the user searches for; don't know how I'd do that before page_load.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Well, you're going to have to carefully consider the life cycle of the page, especially when using one page to do multiple jobs like accepting a search form and displaying the results. The bottom line is that you can't handle an event from a control that hasn't been created at the time events are processed, and that doesn't have the same ID it had on the page that posted the event back to the server. It's been awhile since I did any intensive ASP, so maybe someone else here can give you some ideas. My recommendation would be to get a really good handle on the flow as a page is created. Remember that it is recreated on each request. There's no state other than what is persisted in the view state, or what you persist in session variables. So you have to line things up right so that the engine knows what control to pass an event to. Pretty certain that's your problem.