RadioButtonList:OnSelectedIndexChanged没有触发

时间:2011-07-07 12:15:29

标签: c# asp.net radiobuttonlist

我有一个aspx页面,我用OnSelectedIndexChanged事件动态添加radiobuttonlist。如果我检查所选项目。我有2件物品。

对于第一个项目,事件触发良好,但是如果我选择其他选项,则事件不会触发:代码下方..

事件只是解雇是我从“一些提供”改为“全部提供”另一种方式它不工作

添加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;

检查所选项目:

   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;
   }

非常感谢任何帮助和评论

4 个答案:

答案 0 :(得分:5)

这适用于从Google发现此问题的人:

RadioButtonList上,将AutoPostBack媒体资源设为true

RadioButtonList OnSelectedIndexChanged

答案 1 :(得分:2)

我有这个问题并解决了它。

要提高RadioButtonList的onselectedindexchanged事件,请检查以下项目:

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

并在Page_Load中设置代码:

     rdlCondition.AutoPostBack = true;
     rdlCondition.SelectedIndexChanged += new EventHandler (rdlCondition_SelectedIndexChanged);

答案 2 :(得分:1)

我已经制作了一个示例aspx页面,并在.aspx中添加了一个面板,如下所示:

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

在代码背后,我添加了以下代码:

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")
       {

       }

    }

事件每次都在点亮单选按钮listitem

所以,我担心,你在其他地方做错了什么。祝你好运。

答案 3 :(得分:1)

查看上面的代码,似乎有很多代码重用。我重新组织了你的代码(假设你没有留下任何东西)。请记住,我从未测试过它。

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;
   }
}

我知道这对你当前的问题没有帮助,但这只是一个建议。如果您可以将实际添加值的代码发布到列表中,我可以提供更多帮助。

编辑:您的问题可能是您没有设置商品的价值,只能设置文字。请尝试使用rbl_MinCriteria.SelectedItem.Text =="All provided"