I can not get into the event when I click the dynamically created button












0















I am trying to make the program, with the click of the button, enter the event but I can't, can someone help me?



The "foreach" is working but it does not enter and I don't know why.



when the button is clicked it doesn't start the event but when it's clicked twice all buttons desapear



foreach (var Cat in db.Categorias) {
var id_Categoria = db.Categorias.Where(x => x.IdCategoria.Equals(Cat.IdCategoria)).FirstOrDefault();
if (id_Categoria != null && Cat.IdCategoria.Equals(Cat.IdCategoria)) {
UpdatePanel UP_btn_Cat = new UpdatePanel();
UP_btn_Cat.ID = "UP_btn_Cat_" + Cat.IdCategoria.ToString();
UP_btn_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
Plh_cat.Controls.Add(UP_btn_Cat);

Button btn_cat = new Button();
btn_cat.Text = Cat.NomeCategoria;
btn_cat.ID = "btn_cat_" + Cat.IdCategoria.ToString();
btn_cat.Click += new EventHandler(button_Click);
UP_btn_Cat.ContentTemplateContainer.Controls.Add(btn_cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "button_Click";
UP_btn_Cat.Triggers.Add(trigger);

Plh_email.Controls.Add(new LiteralControl("<div style='clear: both'></div>"));
}
}

protected void button_Click(object sender, EventArgs e) {
Button btn_cat = (Button)sender;

PlaceHolder plh_msg_cat = new PlaceHolder();
plh_msg_cat.ID = "plh_msg_cat";

UpdatePanel UP_mgs_Cat = new UpdatePanel();
UP_mgs_Cat.ID = "UP_mgs_Cat";
UP_mgs_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
div_msgs.Controls.Add(UP_mgs_Cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "Click";
UP_mgs_Cat.Triggers.Add(trigger);

var id = btn_cat.ID;
Plh_cat.Controls.Add(new LiteralControl("<p class='p_cliente'>123" + id + "</p>"));
}









share|improve this question

























  • is your code working ? aren't you getting error in the foor loop ?

    – Waleed Naveed
    Nov 23 '18 at 11:52











  • @WaleedNaveed my code works fine!

    – Leadnnd Pereira
    Nov 23 '18 at 14:31


















0















I am trying to make the program, with the click of the button, enter the event but I can't, can someone help me?



The "foreach" is working but it does not enter and I don't know why.



when the button is clicked it doesn't start the event but when it's clicked twice all buttons desapear



foreach (var Cat in db.Categorias) {
var id_Categoria = db.Categorias.Where(x => x.IdCategoria.Equals(Cat.IdCategoria)).FirstOrDefault();
if (id_Categoria != null && Cat.IdCategoria.Equals(Cat.IdCategoria)) {
UpdatePanel UP_btn_Cat = new UpdatePanel();
UP_btn_Cat.ID = "UP_btn_Cat_" + Cat.IdCategoria.ToString();
UP_btn_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
Plh_cat.Controls.Add(UP_btn_Cat);

Button btn_cat = new Button();
btn_cat.Text = Cat.NomeCategoria;
btn_cat.ID = "btn_cat_" + Cat.IdCategoria.ToString();
btn_cat.Click += new EventHandler(button_Click);
UP_btn_Cat.ContentTemplateContainer.Controls.Add(btn_cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "button_Click";
UP_btn_Cat.Triggers.Add(trigger);

Plh_email.Controls.Add(new LiteralControl("<div style='clear: both'></div>"));
}
}

protected void button_Click(object sender, EventArgs e) {
Button btn_cat = (Button)sender;

PlaceHolder plh_msg_cat = new PlaceHolder();
plh_msg_cat.ID = "plh_msg_cat";

UpdatePanel UP_mgs_Cat = new UpdatePanel();
UP_mgs_Cat.ID = "UP_mgs_Cat";
UP_mgs_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
div_msgs.Controls.Add(UP_mgs_Cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "Click";
UP_mgs_Cat.Triggers.Add(trigger);

var id = btn_cat.ID;
Plh_cat.Controls.Add(new LiteralControl("<p class='p_cliente'>123" + id + "</p>"));
}









share|improve this question

























  • is your code working ? aren't you getting error in the foor loop ?

    – Waleed Naveed
    Nov 23 '18 at 11:52











  • @WaleedNaveed my code works fine!

    – Leadnnd Pereira
    Nov 23 '18 at 14:31
















0












0








0








I am trying to make the program, with the click of the button, enter the event but I can't, can someone help me?



The "foreach" is working but it does not enter and I don't know why.



when the button is clicked it doesn't start the event but when it's clicked twice all buttons desapear



foreach (var Cat in db.Categorias) {
var id_Categoria = db.Categorias.Where(x => x.IdCategoria.Equals(Cat.IdCategoria)).FirstOrDefault();
if (id_Categoria != null && Cat.IdCategoria.Equals(Cat.IdCategoria)) {
UpdatePanel UP_btn_Cat = new UpdatePanel();
UP_btn_Cat.ID = "UP_btn_Cat_" + Cat.IdCategoria.ToString();
UP_btn_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
Plh_cat.Controls.Add(UP_btn_Cat);

Button btn_cat = new Button();
btn_cat.Text = Cat.NomeCategoria;
btn_cat.ID = "btn_cat_" + Cat.IdCategoria.ToString();
btn_cat.Click += new EventHandler(button_Click);
UP_btn_Cat.ContentTemplateContainer.Controls.Add(btn_cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "button_Click";
UP_btn_Cat.Triggers.Add(trigger);

Plh_email.Controls.Add(new LiteralControl("<div style='clear: both'></div>"));
}
}

protected void button_Click(object sender, EventArgs e) {
Button btn_cat = (Button)sender;

PlaceHolder plh_msg_cat = new PlaceHolder();
plh_msg_cat.ID = "plh_msg_cat";

UpdatePanel UP_mgs_Cat = new UpdatePanel();
UP_mgs_Cat.ID = "UP_mgs_Cat";
UP_mgs_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
div_msgs.Controls.Add(UP_mgs_Cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "Click";
UP_mgs_Cat.Triggers.Add(trigger);

var id = btn_cat.ID;
Plh_cat.Controls.Add(new LiteralControl("<p class='p_cliente'>123" + id + "</p>"));
}









share|improve this question
















I am trying to make the program, with the click of the button, enter the event but I can't, can someone help me?



The "foreach" is working but it does not enter and I don't know why.



when the button is clicked it doesn't start the event but when it's clicked twice all buttons desapear



foreach (var Cat in db.Categorias) {
var id_Categoria = db.Categorias.Where(x => x.IdCategoria.Equals(Cat.IdCategoria)).FirstOrDefault();
if (id_Categoria != null && Cat.IdCategoria.Equals(Cat.IdCategoria)) {
UpdatePanel UP_btn_Cat = new UpdatePanel();
UP_btn_Cat.ID = "UP_btn_Cat_" + Cat.IdCategoria.ToString();
UP_btn_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
Plh_cat.Controls.Add(UP_btn_Cat);

Button btn_cat = new Button();
btn_cat.Text = Cat.NomeCategoria;
btn_cat.ID = "btn_cat_" + Cat.IdCategoria.ToString();
btn_cat.Click += new EventHandler(button_Click);
UP_btn_Cat.ContentTemplateContainer.Controls.Add(btn_cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "button_Click";
UP_btn_Cat.Triggers.Add(trigger);

Plh_email.Controls.Add(new LiteralControl("<div style='clear: both'></div>"));
}
}

protected void button_Click(object sender, EventArgs e) {
Button btn_cat = (Button)sender;

PlaceHolder plh_msg_cat = new PlaceHolder();
plh_msg_cat.ID = "plh_msg_cat";

UpdatePanel UP_mgs_Cat = new UpdatePanel();
UP_mgs_Cat.ID = "UP_mgs_Cat";
UP_mgs_Cat.UpdateMode = UpdatePanelUpdateMode.Conditional;
div_msgs.Controls.Add(UP_mgs_Cat);

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn_cat.ID;
trigger.EventName = "Click";
UP_mgs_Cat.Triggers.Add(trigger);

var id = btn_cat.ID;
Plh_cat.Controls.Add(new LiteralControl("<p class='p_cliente'>123" + id + "</p>"));
}






c# html asp.net events foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:39









J. Doe

797




797










asked Nov 23 '18 at 11:47









Leadnnd PereiraLeadnnd Pereira

14




14













  • is your code working ? aren't you getting error in the foor loop ?

    – Waleed Naveed
    Nov 23 '18 at 11:52











  • @WaleedNaveed my code works fine!

    – Leadnnd Pereira
    Nov 23 '18 at 14:31





















  • is your code working ? aren't you getting error in the foor loop ?

    – Waleed Naveed
    Nov 23 '18 at 11:52











  • @WaleedNaveed my code works fine!

    – Leadnnd Pereira
    Nov 23 '18 at 14:31



















is your code working ? aren't you getting error in the foor loop ?

– Waleed Naveed
Nov 23 '18 at 11:52





is your code working ? aren't you getting error in the foor loop ?

– Waleed Naveed
Nov 23 '18 at 11:52













@WaleedNaveed my code works fine!

– Leadnnd Pereira
Nov 23 '18 at 14:31







@WaleedNaveed my code works fine!

– Leadnnd Pereira
Nov 23 '18 at 14:31














1 Answer
1






active

oldest

votes


















0














I'm not sure how everything is created but I would suggest you read up on the Page Life Cycle. It states that all controls must be created during the PreInit event. Use the Init to initialize control properties.



If the controls are created only once during !IsPostBack or in the wrong event, then the Click event of the button will not fire.






share|improve this answer
























  • the button is working fine but the event doesn't start.

    – Leadnnd Pereira
    Nov 23 '18 at 14:34











  • @LeadnndPereira if the event doesn't get fired, then the button doesn't work...

    – the_lotus
    Nov 23 '18 at 15:00











  • You help me, please?

    – Leadnnd Pereira
    Nov 23 '18 at 15:12











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',
autoActivateHeartbeat: false,
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%2f53446136%2fi-can-not-get-into-the-event-when-i-click-the-dynamically-created-button%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I'm not sure how everything is created but I would suggest you read up on the Page Life Cycle. It states that all controls must be created during the PreInit event. Use the Init to initialize control properties.



If the controls are created only once during !IsPostBack or in the wrong event, then the Click event of the button will not fire.






share|improve this answer
























  • the button is working fine but the event doesn't start.

    – Leadnnd Pereira
    Nov 23 '18 at 14:34











  • @LeadnndPereira if the event doesn't get fired, then the button doesn't work...

    – the_lotus
    Nov 23 '18 at 15:00











  • You help me, please?

    – Leadnnd Pereira
    Nov 23 '18 at 15:12
















0














I'm not sure how everything is created but I would suggest you read up on the Page Life Cycle. It states that all controls must be created during the PreInit event. Use the Init to initialize control properties.



If the controls are created only once during !IsPostBack or in the wrong event, then the Click event of the button will not fire.






share|improve this answer
























  • the button is working fine but the event doesn't start.

    – Leadnnd Pereira
    Nov 23 '18 at 14:34











  • @LeadnndPereira if the event doesn't get fired, then the button doesn't work...

    – the_lotus
    Nov 23 '18 at 15:00











  • You help me, please?

    – Leadnnd Pereira
    Nov 23 '18 at 15:12














0












0








0







I'm not sure how everything is created but I would suggest you read up on the Page Life Cycle. It states that all controls must be created during the PreInit event. Use the Init to initialize control properties.



If the controls are created only once during !IsPostBack or in the wrong event, then the Click event of the button will not fire.






share|improve this answer













I'm not sure how everything is created but I would suggest you read up on the Page Life Cycle. It states that all controls must be created during the PreInit event. Use the Init to initialize control properties.



If the controls are created only once during !IsPostBack or in the wrong event, then the Click event of the button will not fire.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 13:46









the_lotusthe_lotus

10.1k12246




10.1k12246













  • the button is working fine but the event doesn't start.

    – Leadnnd Pereira
    Nov 23 '18 at 14:34











  • @LeadnndPereira if the event doesn't get fired, then the button doesn't work...

    – the_lotus
    Nov 23 '18 at 15:00











  • You help me, please?

    – Leadnnd Pereira
    Nov 23 '18 at 15:12



















  • the button is working fine but the event doesn't start.

    – Leadnnd Pereira
    Nov 23 '18 at 14:34











  • @LeadnndPereira if the event doesn't get fired, then the button doesn't work...

    – the_lotus
    Nov 23 '18 at 15:00











  • You help me, please?

    – Leadnnd Pereira
    Nov 23 '18 at 15:12

















the button is working fine but the event doesn't start.

– Leadnnd Pereira
Nov 23 '18 at 14:34





the button is working fine but the event doesn't start.

– Leadnnd Pereira
Nov 23 '18 at 14:34













@LeadnndPereira if the event doesn't get fired, then the button doesn't work...

– the_lotus
Nov 23 '18 at 15:00





@LeadnndPereira if the event doesn't get fired, then the button doesn't work...

– the_lotus
Nov 23 '18 at 15:00













You help me, please?

– Leadnnd Pereira
Nov 23 '18 at 15:12





You help me, please?

– Leadnnd Pereira
Nov 23 '18 at 15:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446136%2fi-can-not-get-into-the-event-when-i-click-the-dynamically-created-button%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python