开发者

Add javascript onclick to radio button INPUT tag

I have a javascript function and need to add it to some dynam开发者_如何学Goically 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.


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.


just use this javascript function, you don't need to explicit add it to the tags.

http://www.mediaevent.de/javascript/event_listener.html

newRadioSelect.addEventListener("click", myFunction, false); 

function myFunction(event){
    toggle(event.target);
}


You can add attributes to your dynamic RadioButton in this fashion

rb.Attributes["onClick"] = "javascript:alert('Hi');";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜