简体   繁体   中英

C# Radiobutton oncheckchanged not firing

I have a radiobutton control dynamically generated inside of a repeater. On this radiobutton I am calling OnCheckChanged to change some value in the code behind on checking on the radiobutton.

<EclipseUI:CustomRadioButton ID="RadioButton_Selected" runat="server" GroupName='<%# "s_id_" + Eval("FeaturePackId") %>' 
 OnCheckedChanged="RadioButton_Selected_OnCheckedChanged"/>

My Radiobuttonis above and calls the shown method.

If it makes any difference the radiobutton is being checked and unchecked by jquery code on a div click.

protected void RadioButton_Selected_OnCheckedChanged(object sender, EventArgs e)
{
    CustomRadioButton rb = (CustomRadioButton) sender;
    System.Diagnostics.Debug.WriteLine(rb.GroupName);
    int FeatureID = Int32.Parse(rb.GroupName.Remove(0, 4));

    if (rb.Checked)
    {
        FeatureList.Add(FeatureID);
    }
    if (!rb.Checked)
    {
        foreach (var itemToRemove in FeatureList)
        {
            FeatureList.Remove(itemToRemove);
            System.Diagnostics.Debug.WriteLine(itemToRemove);
        }
    }
}

public List<int> FeatureList = new List<int>();

This code basically adds of subsracts the value on the radiobuttonto the List of ints. However this method is not being called at all. I have tried placing the debug writeline statements but these do not output anything to the output window.

Does anyone know why this method is not firing?

Try this first because its easy.

Similar question here: OnCheckedChanged event not firing

There are two differences from the posted answer to yours: Enabled="true" AutoPostBack="true"

But since you tried this already I really think it is more likely because you are creating your elements in the wrong place in the page lifecycle (timewise too early) at the pre-page rendering so you may want to create the method binding in the pageload in the non-postback.

In short you have the method and the button but they are not bound to each other.

I am not sure this code will work 'as is', but it might give you some ideas.

    if (!Page.IsPostBack)
      {
         CustomRadioButton rb = (CustomRadioButton)e.Item.FindControl("CustomRadioButtonID");
         rb.OnCheckedChanged += new EventHandler(RadioButton_Selected_OnCheckedChanged);
      }

我认为您应该在CustomRadioButton控件上将PostBack属性设置为TRUE。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM