Search code examples
c#asp.netrepeater

Repeater bind user control event


I have a web user control with a repeater and inside the repeater i have a dynamic number of radiobuttons, On page_load i am trying to call the onDataItemBound event of the repeater to find the radiobuttons and binding the oncheckedchanged event to a method in my code but cant get it to work, here is my code:

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


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

Solution

  • I've managed to solve my problem, the problem was that i have another method being called before this OnItemDataBound which called DataBind() on the repeater, so I simply reversed the order and put the call for OnItemDataBound before the other method like so:

        protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRadioOnChecked();
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetFeaturePackData();
            }
        }
    
        private void BindRadioOnChecked()
        {
            foreach (RepeaterItem item in Repeater_Select.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    CustomRadioButton customRbtn = (CustomRadioButton)item.FindControl("RadioButton_Select");
                    customRbtn.CheckedChanged += RadioButton_Select_OnCheckedChanged;
                }
            }
        }
    

    Now it's inside Page_Init so fires before Page_Load