Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioButtonList: OnSelectedIndexChanged not firing

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

like image 338
Yann Avatar asked Jul 07 '11 12:07

Yann


4 Answers

This is for people who find this question from Google:

On the RadioButtonList, set the AutoPostBack property to true.

RadioButtonList OnSelectedIndexChanged

like image 189
Anthony Graglia Avatar answered Nov 12 '22 02:11

Anthony Graglia


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);
like image 36
Ali Sarshogh Avatar answered Nov 12 '22 01:11

Ali Sarshogh


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.

like image 32
Gage Avatar answered Nov 12 '22 02:11

Gage


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.

like image 27
Bikee Avatar answered Nov 12 '22 01:11

Bikee