3

I have a custom radiobutton below inside of a repeater and when a user clicks it, it is supposed to fire the code behind, however this is not firing at all. I've placed a breakpoint at the beginning of the method and it isnt ever reached. The only thing that does happen is a postback of the updatepanel

<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select"
                                            ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>'
                                            OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True"/>

The code behind is simply this, it takes the value of each checked radiobutton and places it in a hidden field for use later.

protected void RadioButton_Select_OnCheckedChanged(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Clear();

        foreach (RepeaterItem repeaterItem in Repeater_Select.Items)
        {
            CustomRadioButton radioButton = repeaterItem.FindControl("RadioButton_Select") as CustomRadioButton;
            if (radioButton != null)
            {
                if (radioButton.Checked)
                {
                    sb.Append(radioButton.GroupName.Substring(4));
                }
                else
                {
                    sb.Replace(radioButton.GroupName.Substring(4), "");
                }
            }
        }

        HF_Feature.Value = sb.ToString();
    }

My problem is that this doesnt fire at all.

EDIT

My code is now as follows, and the item databound event is hit and so is the += assignment of the eventhandler to the radiobutton but clicking the radiobutton still doesnt hit my breakpoint in the OnCheckedChange of the radiobutton method:

protected void Page_Load(object sender, EventArgs e)
    {
        Repeater_Select.ItemDataBound += new RepeaterItemEventHandler(Repeater_Select_OnItemDataBound);
    }

protected void Repeater_Select_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
            customRbtn.CheckedChanged += new EventHandler(RadioButton_Select_OnCheckedChanged);
        }
    }

But it is still not working

user667430
  • 1,487
  • 8
  • 38
  • 73
  • The MSDN page here names the method "_CheckedChange" in the example. May be worth a try. http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.oncheckedchanged(v=vs.110).aspx – BCza Aug 07 '14 at 14:48
  • @PolkaDancer, the name of the method doesn't matter. OP could name it "elephant" if he/she wanted to. – j.f. Aug 07 '14 at 14:50
  • If memory serves me correctly I came accros this same issue several years ago and put it down to a bug with updatepanels. Try removing the update panel and see if it starts working. If it does my solution I think was to move the the checkbox outside the update panel and wiring it up using the asyncpostbacktriggers property. My longer term fix was to never use update panels ;-) – Ben Robinson Aug 07 '14 at 15:07
  • How are you binding your repeater? May we see the code for that? – j.f. Aug 07 '14 at 15:09
  • add a breakpoint in the item databound, on the "if" line... is it getting hit? if not, are you triggering the repeaters databind method anywhere? you should be. – FlemGrem Aug 07 '14 at 15:13
  • @j.f. I am binding my repeater using a `List` object and simply setting the datasource to this and then calling `DataBind()` inside of `PageLoad()` – user667430 Aug 07 '14 at 15:31
  • Does it work if you use a standard ` – j.f. Aug 07 '14 at 15:45
  • wire up the myRepeater.ItemDataBound in the page_init - earlier than page load. as per my example. – FlemGrem Aug 07 '14 at 15:48
  • @j.f. I cant really use a standard asp radiobutton as i am using it inside of a repeater, the naming bug will create unique IDs which will interfere with my client side javascript – user667430 Aug 07 '14 at 15:52

2 Answers2

1

in a repeater you will have to wire up the OnCheckedChanged events inside of the repeaters _ItemDataBound.

 protected void Page_Init(object sender, EventArgs e
{
   myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound);
}

and then..

private void myRepeater_ItemDataBound(object sender, RepeaterCommandEventArgs e)
{
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
       ListItemType.AlternatingItem)
    {

        CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
        //Now you have an instance of your eclipse radio button so you can do what you want with it.

    }
}

Have a looksy at this: OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked at at way the "_ItemDataBound" is wired up.

and this article may be a little closer to your problem if your using item template in the .aspx file. http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm

Community
  • 1
  • 1
FlemGrem
  • 814
  • 4
  • 9
  • 1
    The statement that you _have_ to do it in the `ItemDataBound` is false. A simple bare bones test can prove that. – j.f. Aug 07 '14 at 15:20
  • @j.f -IMHO controls inside of a repeater don't automatically wireup their events; so an onCheckChanged Event (a radiobutton, inside of a repeater) can only be hooked into if wired up in the _itemDataBound of the repeater. (Asp.net 2.0 ) – FlemGrem Aug 07 '14 at 15:32
  • 2
    @AaronH: `AutomaticEventWireup` won't work within a repeater, but wiring up the events from the markup using an `OnEventName="Handler"` attribute works perfectly well. – Richard Deeming Aug 07 '14 at 15:50
  • @AaronH, even in .NET 2.0 (making the assumption OP is using .NET 2.0 isn't the greatest idea in the first place, IMO), wiring up the handlers like Richard explains (in the markup), would still work. Again, a simple test can prove this. – j.f. Aug 07 '14 at 15:56
  • RD, thank you for the info. j.f that's a great example for you to learn from on how to be constructive. "A simple bare bones test can prove that." is a very lazy response. its like.. I can do that.. well done! isn't the world a nicer place with helpful advice. – FlemGrem Aug 07 '14 at 15:57
  • @AaronH, I agree, it was lazy. I apologize. I was just following the markup in OP's question. – j.f. Aug 07 '14 at 16:00
0

Just try to setting CauseValidation="false" (besides Autopostback="true")

<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select" ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>' OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True" CauseValidation="false"/>

It works...

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60