RadioButtonList: OnSelectedIndexChanged not firing
Asked Answered
C

4

6

I have an aspx page where i dynamically add a radiobuttonlist with OnSelectedIndexChanged event. In the event i check for the selected items. i have 2 items.

For the first item,the event is firing well, However if i choose the other option the event is not firing: below the code..

The event is only firing is i change from "Some provided" to "All provided" the other way it is not working

Adding the RBL:

                RadioButtonList dControl_b = new RadioButtonList();
                dControl_b.ID = "rbl_MinCriteria";
                dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
                dControl_b.CssClass = "Font";
                dControl_b.Font.Name = "Arial";
                dControl_b.Font.Size = 8;
                dControl_b.ToolTip = "";
                dControl_b.SelectedIndex = -1;
                dControl_b.SelectedIndexChanged += new EventHandler(rbl_MinCriteria_SelectedIndexChanged);
                dControl_b.AutoPostBack = true;

Checking the selected item:

   if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
   {
       cbl_MinimumCriteria.Items[0].Selected = true;
       cbl_MinimumCriteria.Items[1].Selected = true;
       cbl_MinimumCriteria.Items[2].Selected = true;
       cbl_MinimumCriteria.Items[3].Selected = true;
       cbl_MinimumCriteria.Enabled = false;

       //*************************************************************
       if (ddl_CountryOccurence.SelectedValue != "Please choose")
       {
           ddl_CountryOccurence.Enabled = false;
       }
       else
       {
           ddl_CountryOccurence.Enabled = true;
       }

       //*************************************************************
       if (tb_DueDate.Text != "")
       {
           tb_DueDate.Enabled = false;
       }
       else
       {
           tb_DueDate.Enabled = true;
       }

       OtherControlI.Enabled = false;
       OtherControlII.Enabled = false;
       OtherControlIII.Enabled = false;
   }
   if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
   {
       cbl_MinimumCriteria.Items[0].Selected = false;
       cbl_MinimumCriteria.Items[1].Selected = false;
       cbl_MinimumCriteria.Items[2].Selected = false;
       cbl_MinimumCriteria.Items[3].Selected = false;
       cbl_MinimumCriteria.Enabled = true;

       //*************************************************************
       if (ddl_CountryOccurence.SelectedValue != "Please choose")
       {
           ddl_CountryOccurence.Enabled = false;
       }
       else
       {
           ddl_CountryOccurence.Enabled = true;
       }

       //*************************************************************
       if (tb_DueDate.Text != "")
       {
           tb_DueDate.Enabled = false;
       }
       else
       {
           tb_DueDate.Enabled = true;
       }

       OtherControlI.Enabled = false;
       OtherControlI.SelectedIndex = -1;
       OtherControlII.Enabled = false;
       OtherControlII.SelectedIndex = -1;
       OtherControlIII.Enabled = false;
       OtherControlIII.SelectedIndex = -1;
   }

Any help and Comment is much appreciated

Chamonix answered 7/7, 2011 at 12:15 Comment(2)
I suspect that you are adding your options to RadioButtonList with the same values. Can you post the code that adds items to RadioButtonList.Hedy
See my answer below. I added it a little late but this result is high in Google so figured it would help.Recommend
R
5

This is for people who find this question from Google:

On the RadioButtonList, set the AutoPostBack property to true.

RadioButtonList OnSelectedIndexChanged

Recommend answered 26/7, 2012 at 14:30 Comment(0)
B
4

I have this problem and solved it.

For raising onselectedindexchanged event of RadioButtonList , check below items:

       <asp:RadioButtonList ID="rdlCondition" runat="server" AutoPostBack="True" 
        onselectedindexchanged="rdlCondition_SelectedIndexChanged">

and in the Page_Load set them with code :

     rdlCondition.AutoPostBack = true;
     rdlCondition.SelectedIndexChanged += new EventHandler (rdlCondition_SelectedIndexChanged);
Birdsong answered 2/7, 2013 at 4:6 Comment(0)
C
1

I've made a sample aspx page, and added one panel in .aspx like below:

<asp:Panel ID="Panel1" runat="server"></asp:Panel>

And in code behind, I've added following code:

protected void Page_Load(object sender, EventArgs e)
    {
        RadioButtonList dControl_b = new RadioButtonList();
        dControl_b.ID = "rbl_MinCriteria";
        dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
        dControl_b.CssClass = "Font";
        dControl_b.Font.Name = "Arial";
        dControl_b.Font.Size = 8;
        dControl_b.ToolTip = "";
        dControl_b.SelectedIndex = -1;
        dControl_b.SelectedIndexChanged += new          EventHandler(rbl_MinCriteria_SelectedIndexChanged);
        dControl_b.AutoPostBack = true;

        dControl_b.Items.Add(new ListItem("All provided"));
        dControl_b.Items.Add(new ListItem("Some provided"));

        Panel1.Controls.Add(dControl_b);
    }
    protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
    {
        RadioButtonList rbl_MinCriteria = (RadioButtonList)Panel1.FindControl("rbl_MinCriteria");
       if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
       {

       }
       if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
       {

       }

    }

The event is FIRING EVERY TIME the radio button listitem is changed.

So, I'm afraid, you have done something wrong elsewhere. Good luck.

Contexture answered 7/7, 2011 at 12:35 Comment(2)
Hi Bikash, the only difference between your code and mine ist that i create the control in a seperate function. But this function is also called in the page load event. What i simply does not understand why the event is firing if i choos "all provided" but no if i choose "some provided" the event is corelated to a control, and not to the selected itemChamonix
Hi Yann, have you ran my code? If not, then try it by creating a new website. Just paste my code in code-behind, put breakpoints in each if statement and see if it hits there or not. I am sure it will hit in every event fire. This way you will find that the problem lies elsewhere. Then add more code snippet and try again. Or else send me your code.Contexture
Y
1

Looking at the code above there seems to be alot of code reuse. I reorganized your code a bit (assuming you didnt leave anything out). Keep in mind I never tested it.

protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
{
   if (rbl_MinCriteria.SelectedIndex<0) return; //If nothing is selected then do nothing

       OtherControlI.Enabled = false;
       OtherControlII.Enabled = false;
       OtherControlIII.Enabled = false;
   if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
   {
       cbl_MinimumCriteria.Items[0].Selected = true;
       cbl_MinimumCriteria.Items[1].Selected = true;
       cbl_MinimumCriteria.Items[2].Selected = true;
       cbl_MinimumCriteria.Items[3].Selected = true;
       cbl_MinimumCriteria.Enabled = false;

   }
   if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
   {
       cbl_MinimumCriteria.Items[0].Selected = false;
       cbl_MinimumCriteria.Items[1].Selected = false;
       cbl_MinimumCriteria.Items[2].Selected = false;
       cbl_MinimumCriteria.Items[3].Selected = false;
       cbl_MinimumCriteria.Enabled = true;

       OtherControlI.SelectedIndex = -1;
       OtherControlII.SelectedIndex = -1;
       OtherControlIII.SelectedIndex = -1;
   }

   //*************************************************************
   if (ddl_CountryOccurence.SelectedValue != "Please choose")
   {
       ddl_CountryOccurence.Enabled = false;
   }
   else
   {
       ddl_CountryOccurence.Enabled = true;
   }
   //*************************************************************
   if (tb_DueDate.Text != "")
   {
     tb_DueDate.Enabled = false;
   }
   else
   {
       tb_DueDate.Enabled = true;
   }
}

I know this doesn't help your current problem but this is just a suggestion. If you could post the code where your actually adding the values to the list I could help a bit more.

EDIT: Your problem could be your not setting the value of your items, only the text. Try using rbl_MinCriteria.SelectedItem.Text =="All provided" instead.

Yellows answered 7/7, 2011 at 13:1 Comment(11)
I intermittently added values to the list items. But it is still not working! I also startet completely from scratch, deleted the code to create the list and wrote it from the beginning. Still the same problemChamonix
@Yann, Did you try using SelectedItem.Text? How are you adding items to the list? Can you post the code?Yellows
this is how i add the items: ListItem a = new ListItem(); a.Value = "All provided"; a.Text = "All provided"; ListItem b = new ListItem(); b.Value = "Some provided"; b.Text = "Some provided"; And i tried both, SelectedItems AND selectedValue dControl_b.Items.Add(a); dControl_b.Items.Add(b);Chamonix
i also have another RBl with postback in the same form.. this one is working perfectly. only this new RBL id DRIVING ME CRAZY.. AHHHH :DChamonix
@Yann, Are you calling dControl_b.Databind() after adding the items? If you debug the application it only hits the breakpoint when you switch values one way and not the other?Yellows
i added dControl_b.Databind() but this did not change anything. But sorry, i didn't get you on the other point.Chamonix
@Yann, Put a breakpoint in your SelectedIndexChanged event(Click the area to the left of the code) and see if it gets hit when you switch between your items. If it is then the problem lies with your if statements.Yellows
It got hit only if i change from "Some provided" to "All provided" If i switch from "All provided" to "Some provided" the event does not fire so the breakpoint is not hit.Chamonix
@Yann, Someone in the post here had the same problem as you. forums.asp.net/t/1076253.aspx/… Do you have the viewState turned off on your page/control. That could be what is causing this to happen. Also another solution could be to set the selected index to -1 after you add your items to the list.Yellows
Hi All, even with all your help i did not manage this control to work properly. I changed a bit the flow on my form and know use a checkbox. Nevertheless thanks for all your help!!!Chamonix
This is not an answer... reformatting and 'could be' does not help and takes a lot of space to say nothing helpful.Recommend

© 2022 - 2024 — McMap. All rights reserved.