Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is rather interesting issue I am facing at the moment. I have a radio button list on my page with three options "Yes", "No" and "Maybe". It has AutoPostBack set to true to fire SelectedIndexChanged event.

<asp:RadioButtonList ID="rblTakeHoliday" runat="server" AutoPostBack="True"
                    onselectedindexchanged="rblTakeHoliday_SelectedIndexChanged"
                    RepeatDirection="Horizontal">
                    <asp:ListItem Value="Yes">Yes</asp:ListItem>
                    <asp:ListItem Value="No">No</asp:ListItem>
           <asp:ListItem Value="Maybe">Maybe</asp:ListItem>
                </asp:RadioButtonList>


User selects one of the option from RadioButtonList and based on selection four gridviews are populated with data and user can select some rows from those gridviews and submit this information. Up to this point it works fine.

If user later come to same page again and want to change holiday preference from whatever selected before to something new I want to display his previous holiday selection. So I fetch his previous selection from database and make one of the radio button selected in code behind as follow (For example user selected "Maybe" as an option initially):

rblTakeHoliday.Items[0].Selected = false;
rblTakeHoliday.Items[1].Selected = false;
rblTakeHoliday.Items[2].Selected = true;


Now user is playing with those three radio buttons on the page. The SelectedIndexChanged event will fire when user selects "Yes" or "No" radio button but it will not fire for "Maybe" radio button as it rendered with checked="checked" in browser because I set it as Selected= "true" in code behind. So for example If user selects option "Yes" the SelectedIndexChanged event will fire. If user go back to "Maybe" option the SelectedIndexChaged event will not get fired. Is there any work around to this ? I want to make radio button list fire its SelectedIndexChanged event on any radio button selection.

I know this is the issue because of setting radio button selected in code behind.
Posted

hi,

Need more details on which state you are setting the values in page behind, is this Page:Load? if so try this

if(!IsPostBack){//SET YOUR RADIO BUTTON LIST DATA}

Another workaround is the way you are setting data, you should use

// Suppose at databse value "MayBe" is saved. Assign it into a variable the try to set
String myValue="MayBe";
rblTakeHoliday.SelectedIndex=
rblTakeHoliday.Items.IndexOf(rblTakeHoliday.Items.FindByValue(myValue));


Hope this will help.
 
Share this answer
 
Apparently it turn out to be an issue with third party AJAX RAD controls ( Telerik Controls and AjaxSettings section of those controls )I am using in the application. I had to move RadioButtonList inside panel and use that panel ID as control of AjaxSettings for Telerik.

I created a test page to test if it happens without AJAX or not and finally able to nail the problem.
 
Share this answer
 
XML
I had a similar experience where the radiobuttonlist would fire on one selection but not when I reselected the orginal selection in the list.  This is a wierd fix, but I had to do this.

<pre lang="c#">
protected void rblTakeHoliday_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem item in radio.Items)
    {
        if (item.Selected)
           item.Selected = true;
    }
}
</pre>
 
Share this answer
 
XML
Get it inside an update panel, with property "ChildrenAsTriggers" on true. This solve the issue for me.

Example:
<pre lang="HTML">
<asp:UpdatePanel ID="updpnlRbtnLocalidad" runat="server" UpdateMode="Conditional" <b>ChildrenAsTriggers="true"</b>>
  <ContentTemplate>
    <asp:Panel ID="pnlrbtnLocalidad" runat="server">
      <asp:RadioButtonList ID="rbtnLocalidad" runat="server" AutoPostBack="true"   OnSelectedIndexChanged="rbtnLocalidad_SelectedIndexChanged">
        <asp:ListItem Text="Lugar" Value="4"></asp:ListItem>
        <asp:ListItem Text="Pais" Value="3"></asp:ListItem>
        <asp:ListItem Text="Provincia" Value="2"></asp:ListItem>
        <asp:ListItem Text="Departamento" Value="1"></asp:ListItem>
        <asp:ListItem Text="Localidad" Value="0"></asp:ListItem>
      </asp:RadioButtonList>
    </asp:Panel>
  </ContentTemplate>
</asp:UpdatePanel>
</pre>
 
Share this answer
 
Comments
asmaaelshabaka 6-May-14 8:30am    
i did that but still does not postback
I have the same problem


can some one help me i have three items in my radiobuttonlist only the first and second item cam post back but the third item does not postback
 
Share this answer
 
Comments
CHill60 6-May-14 8:58am    
If you have a question of your own then use the "Ask a question" link. Not many people will see your question if you post it as a solution to an old question, and you will get downvoted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900