Client event to dynamic created RadioButtonList

1 Answer 71 Views
RadioButtonList
Omar
Top achievements
Rank 3
Iron
Iron
Iron
Omar asked on 24 Oct 2023, 10:24 AM

Hi,

I want to attach the client event to a dynamically created radio list.

Below is an example of a dynamically created radio list.

The question is, How to attach the JS function to OnSelectedIndexChanged, to display the selected radio value?

Many thanks in advance.

Omar

 

                RadRadioButtonList RBL = new RadRadioButtonList();
                RBL.ID = "Radio" + i.ToString();
                RBL.AutoPostBack = false;

                ButtonListItem answer1 = new ButtonListItem();
                answer1.Text = "Answer " + i.ToString();
                answer1.Value = i.ToString();
                RBL.Items.Add(answer1);

                ButtonListItem answer2 = new ButtonListItem();
                answer2.Text = "Answer " + i.ToString();
                answer2.Value = i.ToString();
                RBL.Items.Add(answer2);

                ButtonListItem answer3 = new ButtonListItem();
                answer3.Text = "Answer " + i.ToString();
                answer3.Value = i.ToString();
                RBL.Items.Add(answer3);

                RBL.ClientEvents.OnSelectedIndexChanged = "OnItemClicked";

1 Answer, 1 is accepted

Sort by
1
Accepted
Vasko
Telerik team
answered on 26 Oct 2023, 08:52 AM

Hello Omar,

Assuming you already have a PlaceHolder that will hold the RBL in its Controls collection and a JavaScript function that will be used as the handler for the SelectedIndexChanged event.

<asp:PlaceHolder runat="server" ID="PlaceHolder1" />

<script>
    function OnItemClicked(sender, args) {
        // execute some JavaScript code here
    }
</script>

 

Please keep in mind that when working with dynamic controls, you will have to use the Page PreInit or Init events. This is mentioned in the official Microsoft documentation at ASP.NET Page Life Cycle Events.

protected void Page_PreInit(object sender, EventArgs e)
{
    RadRadioButtonList RBL = new RadRadioButtonList();

    // Set the RBL attributes
    RBL.ClientEvents.OnSelectedIndexChanged = "OnItemClicked";
    RBL.AutoPostBack = false;

    // Create buttons and add them to the RBL button collection

    for (int i = 0; i < 3; i++)
    {
        ButtonListItem answer = new ButtonListItem();
        answer.Text = "Answer " + i.ToString();
        answer.Value = i.ToString();
        RBL.Items.Add(answer);
    }

    // Add the RBL to the PlaceHolder's controls collection
    PlaceHolder1.Controls.Add(RBL);
}

I hope this approach will help resolve the issue.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
RadioButtonList
Asked by
Omar
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Vasko
Telerik team
Share this question
or