Events inside dynamically created user controls do not fire
up vote
0
down vote
favorite
I have a UserControl let's call it ButtonPanel which is simply a button (id="btn") within a panel (id="bPanel"), and a Literal (id="msg"). The button's click event bound to the event handler ("btn_Click") in the ASPX tag for the button. The handler simply puts "Button Pressed" into the aforementioned "msg" Literal.
In my main page, if I drag a ButtonPanel into the page rather than dynamically create it, then when the event is fired within the control, and the message is shown. All works as expected.
However, if I dynamically create the ButtonPanel inside the main page's Page_Init() function and add it to a container on the page, then the btn_Click event handler in the control is never called. The user control's Page_Load() and Page_Init() functions are both called by the PostBack, but the event handler within the UserControl never gets called after that.
One would think that the id of "btn" would be included in the postback data but I have been unable to find it in Request.Params. I wouldn't mind dispatching the event myself if I could just figure out which control within the user control was involved, and what event was involved. After all not everything is a button and a click event.
I've been searching the web and of course i'm far from the first person to run across this. I haven't found any good solutions thus far.
And if anybody from Microsoft is reading this... WTF? ASP.NET has been around for how many years now? Nearly 20, I think? This fundamentally breaks the usefulness of user controls so why haven't you addressed it?
EDITS: Added some detail from subsequent comments.
c# asp.net
add a comment |
up vote
0
down vote
favorite
I have a UserControl let's call it ButtonPanel which is simply a button (id="btn") within a panel (id="bPanel"), and a Literal (id="msg"). The button's click event bound to the event handler ("btn_Click") in the ASPX tag for the button. The handler simply puts "Button Pressed" into the aforementioned "msg" Literal.
In my main page, if I drag a ButtonPanel into the page rather than dynamically create it, then when the event is fired within the control, and the message is shown. All works as expected.
However, if I dynamically create the ButtonPanel inside the main page's Page_Init() function and add it to a container on the page, then the btn_Click event handler in the control is never called. The user control's Page_Load() and Page_Init() functions are both called by the PostBack, but the event handler within the UserControl never gets called after that.
One would think that the id of "btn" would be included in the postback data but I have been unable to find it in Request.Params. I wouldn't mind dispatching the event myself if I could just figure out which control within the user control was involved, and what event was involved. After all not everything is a button and a click event.
I've been searching the web and of course i'm far from the first person to run across this. I haven't found any good solutions thus far.
And if anybody from Microsoft is reading this... WTF? ASP.NET has been around for how many years now? Nearly 20, I think? This fundamentally breaks the usefulness of user controls so why haven't you addressed it?
EDITS: Added some detail from subsequent comments.
c# asp.net
1
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something likebtn.Click += btn_Click
?
– ikerbera
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use theIsPostBack
check when adding Controls.
– VDWWD
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a UserControl let's call it ButtonPanel which is simply a button (id="btn") within a panel (id="bPanel"), and a Literal (id="msg"). The button's click event bound to the event handler ("btn_Click") in the ASPX tag for the button. The handler simply puts "Button Pressed" into the aforementioned "msg" Literal.
In my main page, if I drag a ButtonPanel into the page rather than dynamically create it, then when the event is fired within the control, and the message is shown. All works as expected.
However, if I dynamically create the ButtonPanel inside the main page's Page_Init() function and add it to a container on the page, then the btn_Click event handler in the control is never called. The user control's Page_Load() and Page_Init() functions are both called by the PostBack, but the event handler within the UserControl never gets called after that.
One would think that the id of "btn" would be included in the postback data but I have been unable to find it in Request.Params. I wouldn't mind dispatching the event myself if I could just figure out which control within the user control was involved, and what event was involved. After all not everything is a button and a click event.
I've been searching the web and of course i'm far from the first person to run across this. I haven't found any good solutions thus far.
And if anybody from Microsoft is reading this... WTF? ASP.NET has been around for how many years now? Nearly 20, I think? This fundamentally breaks the usefulness of user controls so why haven't you addressed it?
EDITS: Added some detail from subsequent comments.
c# asp.net
I have a UserControl let's call it ButtonPanel which is simply a button (id="btn") within a panel (id="bPanel"), and a Literal (id="msg"). The button's click event bound to the event handler ("btn_Click") in the ASPX tag for the button. The handler simply puts "Button Pressed" into the aforementioned "msg" Literal.
In my main page, if I drag a ButtonPanel into the page rather than dynamically create it, then when the event is fired within the control, and the message is shown. All works as expected.
However, if I dynamically create the ButtonPanel inside the main page's Page_Init() function and add it to a container on the page, then the btn_Click event handler in the control is never called. The user control's Page_Load() and Page_Init() functions are both called by the PostBack, but the event handler within the UserControl never gets called after that.
One would think that the id of "btn" would be included in the postback data but I have been unable to find it in Request.Params. I wouldn't mind dispatching the event myself if I could just figure out which control within the user control was involved, and what event was involved. After all not everything is a button and a click event.
I've been searching the web and of course i'm far from the first person to run across this. I haven't found any good solutions thus far.
And if anybody from Microsoft is reading this... WTF? ASP.NET has been around for how many years now? Nearly 20, I think? This fundamentally breaks the usefulness of user controls so why haven't you addressed it?
EDITS: Added some detail from subsequent comments.
c# asp.net
c# asp.net
edited 2 days ago
asked 2 days ago
Mike Fulton
650618
650618
1
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something likebtn.Click += btn_Click
?
– ikerbera
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use theIsPostBack
check when adding Controls.
– VDWWD
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago
add a comment |
1
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something likebtn.Click += btn_Click
?
– ikerbera
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use theIsPostBack
check when adding Controls.
– VDWWD
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago
1
1
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something like
btn.Click += btn_Click
?– ikerbera
2 days ago
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something like
btn.Click += btn_Click
?– ikerbera
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use the
IsPostBack
check when adding Controls.– VDWWD
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use the
IsPostBack
check when adding Controls.– VDWWD
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Have you mapped the event handler function to the event?
https://msdn.microsoft.com/en-us/library/t3d01ft1.aspx
When you drag it on to the page and add the click event attribute or code behind handler it does the equivalent of this behind the scenes for you.
When you add it dynamically you have to tell it to map the event to the handler function.
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Have you mapped the event handler function to the event?
https://msdn.microsoft.com/en-us/library/t3d01ft1.aspx
When you drag it on to the page and add the click event attribute or code behind handler it does the equivalent of this behind the scenes for you.
When you add it dynamically you have to tell it to map the event to the handler function.
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
add a comment |
up vote
0
down vote
Have you mapped the event handler function to the event?
https://msdn.microsoft.com/en-us/library/t3d01ft1.aspx
When you drag it on to the page and add the click event attribute or code behind handler it does the equivalent of this behind the scenes for you.
When you add it dynamically you have to tell it to map the event to the handler function.
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Have you mapped the event handler function to the event?
https://msdn.microsoft.com/en-us/library/t3d01ft1.aspx
When you drag it on to the page and add the click event attribute or code behind handler it does the equivalent of this behind the scenes for you.
When you add it dynamically you have to tell it to map the event to the handler function.
Have you mapped the event handler function to the event?
https://msdn.microsoft.com/en-us/library/t3d01ft1.aspx
When you drag it on to the page and add the click event attribute or code behind handler it does the equivalent of this behind the scenes for you.
When you add it dynamically you have to tell it to map the event to the handler function.
answered 2 days ago
SazooCat
625
625
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
add a comment |
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes, of course the event handler is bound to the event. That's done in the ASPX tag for the button:
– Mike Fulton
2 days ago
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Yes it is done in the .aspx page when you add them pre-compile, NOT, if you add them dynamically, e.g. in the Page Init event. As in your comment [quote]When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked[/quote], this is because the html in the browser knows to post back, but you haven't told the compiled server side code what will handle that particular event without mapping the handler as per MSDN link above.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
Having read all your comments a third time... I think you might need your "ButtonPanel" control to have a "BindEvents()" method on it, that reasserts the event bindings in code when the control is added at runtime. It may feel like this is defined by the button panel's layout, but we're still talking about a tree of controls with desired event handlers being added at runtime.
– SazooCat
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
I have tried binding the button's click event to the handler within the Page_Init function of the user control, in addition to it being bound in the tag Makes no difference at all.
– Mike Fulton
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203817%2fevents-inside-dynamically-created-user-controls-do-not-fire%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
It would be nice to see some code of what you got so far. Did you linked the dynamically created button to the click function you made? Something like
btn.Click += btn_Click
?– ikerbera
2 days ago
As @ikerbera mentioned, you need to attach events. AND make sure the Button or Control that triggers the event is still there in a PostBack or it does not work. So do not use the
IsPostBack
check when adding Controls.– VDWWD
2 days ago
Within the user control, the btn_click event handler is bound to the control in the ASPX tag for the button: Within the user control, nothing is dynamically created.
– Mike Fulton
2 days ago
In the main page, the user controls are dynamically created and added to the page within the Page_Init function. There is no test for IsPostBack. When the button in the user control is pressed, The user control's Page_Load handler is invoked, so clearly the system knows some event occurred, but the event handler is not invoked, and a dump of Request.Params fails to turn up any evidence that the ID of the button has been sent from the client.
– Mike Fulton
2 days ago