asp.netvb.netwebformscode-behind

Add javascript onclick to radio button INPUT tag


I have a javascript function and need to add it to some dynamically created radio buttons onclick from code behind.

I've tried - newRadioSelect.Attributes.Add("onlick", "javascript:toggle(this);")

The code above adds it to the span tag that the radio button creates, any ideas how to get it on the input tag of the radio buttons?

Thanks

J.


Solution

  • A bit like buses, nothing for ages then 2 come along at once.

    I worked out a way to do it and got it working and then I received an answer from somewhere else, so thought I'd put them both on here.

    My version (not the best)

            Dim newRadioYes As New RadioButton
            newRadioYes.Text = "Yes"
            newRadioYes.ID = "c_" & childID & "_school_selected_0"
            newRadioYes.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
            newRadioYes.Attributes.Add("value", "Yes")
            newRadioYes.GroupName = "c_" & childID & "_school_selected"
    
            Dim newRadioNo As New RadioButton
            newRadioNo.Text = "No"
            newRadioNo.ID = "c_" & childID & "_school_selected_1"
            newRadioNo.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
            newRadioNo.Attributes.Add("value", "No")
            newRadioNo.GroupName = "c_" & childID & "_school_selected"
    

    Better version

            Dim newRadioSelect As New RadioButtonList
            newRadioSelect.RepeatDirection = RepeatDirection.Horizontal
            newRadioSelect.RepeatLayout = RepeatLayout.Flow
            newRadioSelect.Items.Add("Yes")
            newRadioSelect.Items.Add("No")
            newRadioSelect.Items(0).Attributes.Add("onclick", "javascript:toggle(this);")
            newRadioSelect.Items(1).Attributes.Add("onclick", "javascript:toggle(this);")
    

    Thanks to those who helped.