Adding a new eventHandler to RadioButton CheckedChanges in a dynamic table

I am trying to add another eventHandler to RadioButton . This is a sample code (which works):

ASP.NET:

 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Button ID="Button1" runat="server" Text="Button" /> 

FROM#:

 protected void Page_Load(object sender, EventArgs e) { RadioButton RB1 = new RadioButton(); RB1.ID = "1"; RB1.GroupName = "bla"; RB1.CheckedChanged += new EventHandler(CheckedChanged); RadioButton RB2 = new RadioButton(); RB2.ID = "2"; RB2.GroupName = "bla"; RB2.CheckedChanged += new EventHandler(CheckedChanged); PlaceHolder1.Controls.Add(RB1); PlaceHolder1.Controls.Add(RB2); } protected void CheckedChanged(object sender, EventArgs e) { Label1.Text = ((RadioButton)sender).ID; } 

In my project, I have the dynamic creation of RadioButtons (the number of rows I get from the database). The same eventHandler addition eventHandler not work, but if I write

 MyRadioButton.Load += new EventHandler(Another_method); 

Another_method start, but in

 MyRadioButton.CheckedChanged += new EventHandler(Main_method); 

Main_method will not start if I select one of RadioButtons . What's wrong?


@KevinP This is my code:

  Table tb1 = new Table(); PlaceHolder1.Controls.Add(tb1); TableRow tr = new TableRow(); TableCell tc = new TableCell(); //adding the first row with title" tr.Cells.Add(tc); for (int i = 0; i < ((ArrayList)(result[0])).Count; i++) { tc = new TableCell(); Label example = new Label(); example.Text = ((columnsResultFromSqlClients)(i)).ToString(); tc.Controls.Add(example); tr.Cells.Add(tc); } tb1.Rows.Add(tr); //filling the table for (int i = 0; i < result.Count; i++) { tr = new TableRow(); tc = new TableCell(); //adding radio button RadioButton RB = new RadioButton(); RB.Attributes.Add("value", ((ArrayList)(result[i]))[0].ToString()); RB.GroupName = "for_selecting"; RB.ID = ((ArrayList)(result[i]))[0].ToString(); RB.CheckedChanged += new EventHandler(RB_CheckedChanged2); //RB.AutoPostBack = true; RB.Attributes.Add("AutoPostBack", "True"); tc.Controls.Add(RB); tr.Cells.Add(tc); //adding content for (int j = 0; j < ((ArrayList)(result[i])).Count; j++) { tc = new TableCell(); Label example = new Label(); example.Text = ((ArrayList)(result[i]))[j].ToString(); tc.Controls.Add(example); tr.Cells.Add(tc); } tb1.Rows.Add(tr); } 

If I use RB.AutoPostBack = true; , I don’t have time to press a button to submit my selection, because the page will reload when I press one of the Radio buttons. Also the RB_CheckedChanged2 code:

 protected void RB_CheckedChanged2(object sender, EventArgs e) { RadioButton tempRB = (RadioButton)sender; if (tempRB.Checked) { selected_id = tempRB.ID; } } 

select_id is a static int varible with a standard value = "-1".

+4
source share
1 answer

If I am not mistaken, with dynamic controls in ASP.Net, you need to "rewrite" their events for postback. Remember that there are no more dynamic controls when posting back. You have to recreate them.

+1
source

All Articles