Wednesday, March 28, 2012

Why my dynamically created CheckBoxs CheckedChanged doesnt go off?

Hi!!

I have a Table object, where I Insert dynamically Rows and cells depending on a db data.
In the row, first column is a CheckBox object, I put it in there like this:


CheckBox cb = new CheckBox();
cb.ID = "some value from db query..";
cb.AutoPostBack = true;
cb.CheckedChanged += new EventHandler(MyCheckChange);
tablecell.Controls.Add(cb);
// and so on..

The page loads correctly, but for some reason the CheckedChanged event doesn't go to MyCheckChange() method! It fires off normally otherwise, because the page is posted when I push the checkbox.

Why this could be? I have added "AutoEventWireup="True" " in the beginning of the page. By the way, could someone tell me, what does that AutoEventWireup mean? I just added it, beacuse it seems to be in every msdn's example..

Thankyou in advance!!As this is a dynamic control you will have to recreate it in the Page_Load method. If you don't recreate it .NET can't associate the event with it. Also take care that you recreate it as an exact copy witht eh same ID and everything.

As regards the AutoEventWireup: There are two ways of getting functions in a page to execute. The first is to set AutoEventWireup to true, this instructs .NET to call the usual Page_Load, PreRender etc methods. However if you are using Visual Studio it takes the second option. This involves adding the line


this.Load += new System.EventHandler(this.Page_Load);.

to the InitializeComponent method called from the pages Init method (all this code is generated for and can be seen in the "Web Form Designer Generated Code" section of the code)

As a general rule of thumb, if you are using Visual Studio.NET you should always have AutoEventWireup set to false, if you are not using VS.NET then I guess it is up to you. Just make sure you don't have both AutoEventWireup set to true and the "this.Load += ..." lines set as this causes the methods to be execute twice which can lead to some interesting run time errors

Hope this makes sense
Thanks a lot! Now I got it, why it didn't fire off.. Well, I'm in a real trouble here.

I have about 70 check boxes on a page, and user should check and unchek them, and when he finally pushes "Add" -button, the code should be able to collect all the checkboxes which are checked, and write ids' of those checked cbs' to a db. Now I loose those cb values when I post back. How could I make the values remain after a postback?
I foyu recreate the checkboxes exactly, with the same ID's and everything then they shouldmagically remember their values
Oh yes, so they did! Thanks for your help, petesop1!!

Now I got it to work just fine!
I have the following problem:
Same as you said, so i call a function at the Page_Load witch recreates the EXACTsame CheckBox.
The problem is that the run of the page only goes to the EventHandler Code when the CheckBox is getting on, and it does not go to that piece of code when it goes off.

ASP.NET 1.1
Visual Studio.
Anyone?

0 comments:

Post a Comment