Adding an event handler to a dynamically created checkbox (aspx, C #)

So my problem is that I want to add an event handler to a dynamically created CheckBox. I already looked at other ways to do this and decided that creating a dynamic table containing my CheckBox is the best option for me. I did not add these CheckBoxes to the Management Tree because I need to manage the ViewState manually. In any case, my code works in every way, except that my CheckBox CheckChanged event does not fire. I add this event handler to my CheckBox in my pageLoad event, however any page event that I try seems to give me the same results:

CheckBox chbxLv1 = new CheckBox(); chbxLv1.ID = "DymanicallyCreatedIDForIdentification"; chbxLv1.AutoPostBack = true; chbxLv1.CheckedChanged += new EventHandler(this.checkChanged); /* Way lower in my code */ protected void checkChanged(object sender, EventArgs e) { //Some code goes here which never seems to execute... grrr } 

I thought this might be a problem with ViewState at the beginning and did a lot of research. Now I think I'm doing something dumb with the addition of an event handler. I'm not sure why this event never fires, but I'm a little new to adding events to the control. Do I need a delegate here?

- Roman

+4
source share
1 answer

To correctly handle dynamically loaded controls during the life cycle of an ASP.NET page, they must be added to the page during OnInit (or before LoadViewState), otherwise their status information will not be supported, and you can essentially distort the presentation in depending on how / where things are added in the page management graph.

+2
source

Source: https://habr.com/ru/post/1379549/


All Articles