Dynamic Control Events not Fireing
-
Hi all :) My problem is as follows, I have a ASP Table. Then I add rows/columns in code behind, but one of my columns is a control. I need to see when the Index have changed for specific reasons but the event doesnt fire... Here is my code, something wrong??
//In Page Load TableRow tr = new TableRow(); TableCell tblCell = new TableCell(); RadioButtonList rbL = new RadioButtonList(); rbL.RepeatDirection = RepeatDirection.Horizontal; rbL.SelectedIndexChanged += new EventHandler(rbL_SelectedIndexChanged); rbL.Items.Add("Yes"); rbL.Items.Add("No"); tblCell.Controls.Add(rbL); tr.Cells.Add(tblCell); tblSurvey.Rows.Add(tr); //End of Page Load void rbL_SelectedIndexChanged(object sender, EventArgs e) { //Code here.. }
-
Hi all :) My problem is as follows, I have a ASP Table. Then I add rows/columns in code behind, but one of my columns is a control. I need to see when the Index have changed for specific reasons but the event doesnt fire... Here is my code, something wrong??
//In Page Load TableRow tr = new TableRow(); TableCell tblCell = new TableCell(); RadioButtonList rbL = new RadioButtonList(); rbL.RepeatDirection = RepeatDirection.Horizontal; rbL.SelectedIndexChanged += new EventHandler(rbL_SelectedIndexChanged); rbL.Items.Add("Yes"); rbL.Items.Add("No"); tblCell.Controls.Add(rbL); tr.Cells.Add(tblCell); tblSurvey.Rows.Add(tr); //End of Page Load void rbL_SelectedIndexChanged(object sender, EventArgs e) { //Code here.. }
Unless I miss my guess, I believe that your issue is caused by a change in the automatic ids assigned to controls during each postback. I believe that if assign an explicit value to the
ID
property of yourRadioButtonList
, it will solve your problem. Also, verify that your table has anID
assigned to it, and is being placed in the same container on the page. Hope that helps. :)--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
Unless I miss my guess, I believe that your issue is caused by a change in the automatic ids assigned to controls during each postback. I believe that if assign an explicit value to the
ID
property of yourRadioButtonList
, it will solve your problem. Also, verify that your table has anID
assigned to it, and is being placed in the same container on the page. Hope that helps. :)--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
Hey Jesse, thanx for the quick rely! Ok, I assigned the RadioButtonList' ID explicitly now. But it is still not Fireing for some reason :^) I inserted a breakpoint but it never eners the Event Handler..
-
Hey Jesse, thanx for the quick rely! Ok, I assigned the RadioButtonList' ID explicitly now. But it is still not Fireing for some reason :^) I inserted a breakpoint but it never eners the Event Handler..
It appears that we missed setting the
RadioButtonList
propertyAutoPostback
totrue
. Without that being set, there will be no postback when the value changes. Hope that helps. :)--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
Hi all :) My problem is as follows, I have a ASP Table. Then I add rows/columns in code behind, but one of my columns is a control. I need to see when the Index have changed for specific reasons but the event doesnt fire... Here is my code, something wrong??
//In Page Load TableRow tr = new TableRow(); TableCell tblCell = new TableCell(); RadioButtonList rbL = new RadioButtonList(); rbL.RepeatDirection = RepeatDirection.Horizontal; rbL.SelectedIndexChanged += new EventHandler(rbL_SelectedIndexChanged); rbL.Items.Add("Yes"); rbL.Items.Add("No"); tblCell.Controls.Add(rbL); tr.Cells.Add(tblCell); tblSurvey.Rows.Add(tr); //End of Page Load void rbL_SelectedIndexChanged(object sender, EventArgs e) { //Code here.. }
Your problem is that you need to add controls prior to page_load for their viewstate to be restored. Viewstate is how events are handled. I believe LoadViewState is the place to do this. There are CP articles on this topic.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi all :) My problem is as follows, I have a ASP Table. Then I add rows/columns in code behind, but one of my columns is a control. I need to see when the Index have changed for specific reasons but the event doesnt fire... Here is my code, something wrong??
//In Page Load TableRow tr = new TableRow(); TableCell tblCell = new TableCell(); RadioButtonList rbL = new RadioButtonList(); rbL.RepeatDirection = RepeatDirection.Horizontal; rbL.SelectedIndexChanged += new EventHandler(rbL_SelectedIndexChanged); rbL.Items.Add("Yes"); rbL.Items.Add("No"); tblCell.Controls.Add(rbL); tr.Cells.Add(tblCell); tblSurvey.Rows.Add(tr); //End of Page Load void rbL_SelectedIndexChanged(object sender, EventArgs e) { //Code here.. }
Christian makes a great point that I overlooked. :-O While setting
AutoPostBack
and assigning an id will get your event to fire, you will not be able to query the value of the radio button list. As he mentions, your control is being created too late in the cycle for it to receive its state.ViewState
is restored after the page'sInit
cycle and before theLoad
cycle. You may wish to consider creating your controls either in theOnInit
method or in anInit
event handler.--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
Christian makes a great point that I overlooked. :-O While setting
AutoPostBack
and assigning an id will get your event to fire, you will not be able to query the value of the radio button list. As he mentions, your control is being created too late in the cycle for it to receive its state.ViewState
is restored after the page'sInit
cycle and before theLoad
cycle. You may wish to consider creating your controls either in theOnInit
method or in anInit
event handler.--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
Thanx alot Jesse / Christian!!!!! I now create the coontrols in the Page_Init, this now saves the state and fires me events. :) :) :) :) :) :) :)