'How to deal with a change of Radio Button selection after navigating back?
I have a set of RadioButtons in a repeater. Once a RadioButton is checked, the related repetition is determined in a loop through its items and a parameter is saved to a Data Base. This triggers the loading of the next question with a new set of RadioButtons is. So far so good. If the user navigates back to the previous question then he could post a new selection and mess up everything. To deal with this unwanted operation, I wrote this code for the RadioButton OnChecked event:
All repeaters Radio Buttons are first set back to unchecked and then the latest selection restored with ((RadioButton)sender).Checked = true.
Afterwards I determine the related parameter and overwrite the already existing record for that question with the new value.
protected void RB_Checked(object sender, EventArgs e) { for (int i = 0; i <= RPT_DisplayInquiry.Items.Count - 1; i++) { RadioButton rb = (RadioButton)RPT_DisplayInquiry.Items[i].FindControl("RB_Choice"); rb.Checked = false; } ((RadioButton)sender).Checked = true; for (int i = 0; i <= RPT_DisplayInquiry.Items.Count - 1; i++) { RadioButton rb = (RadioButton)RPT_DisplayInquiry.Items[i].FindControl("RB_Choice"); if (rb.Checked == true) { try { string Choice = Convert.ToString(i + 1); Label lbl = (Label)RPT_DisplayInquiry.Items[i].FindControl("Lbl_Structure"); SaveSelection(lbl.Text, Choice, 0); } catch (Exception ex) { throw ex; } } } }
For some reason this doesn't work. If I navigate back again after the new selection is saved, I see that the reset had not worked and that 2 RadioButtons are selected. Therefore the saved value is not necessarily correct as it is just related to the first found checked RadioButton in the repeater.
If I eliminate the call of the Save Routine from that methode and call it afterwards from a separate click event then the whole thing works.
Does the reset of the RadioButton not work as they are never rendered? What is going on?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
