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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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








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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff