当前位置:编程学习 > C#/ASP.NET >>

asp.net 如何为CheckBoxList控件中的每一个CheckBox 添加事件

asp.net 如何为CheckBoxList控件中的每一个CheckBox 添加事件
我想在CheckBoxList 中的每一个CheckBox添加一个onClick JS 的事件

CheckBoxList.Items[i].Attributes.Add("onClick","test()");
这样写,运行后,查看源代码时,结果:复选框组的每一项都没有添加到"onClick"事件

怎样才能为复选框组的每一项添加一个“点击”事件
请各位高手帮帮忙。 --------------------编程问答-------------------- 在DataBound事件中进行:
    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            CheckBoxList1.Items[i].Attributes.Add("onclick", "jsFunc('"+i.ToString()+"')");
    } --------------------编程问答-------------------- 在DataBound事件中进行:
    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            CheckBoxList1.Items[i].Attributes.Add("onclick", "jsFunc('"+i.ToString()+"')");
    } --------------------编程问答--------------------
引用 2 楼 koukoujiayi 的回复:
在DataBound事件中进行:
    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            CheckBoxList1.Items[i].Attributes.Add("onclick", "jsFunc('"+i.ToString()+"')");
    }

我使用的是.net1.1 
这样子是不行的。 --------------------编程问答-------------------- ??楼主也太落后了吧!学的时候都是2.0的,现在4.0都出来了..... --------------------编程问答-------------------- 在前台用js添加的,checkboxlist最终生成的结果(默认方式)貌似是一个table里面放了N个checkbox的

可以在document ready之后,调用一个js函数来为每一个checkbox来添加js 的onclick事件

--------------------编程问答-------------------- funtion addclick()
{
var options = document.getElementById("checkboxlistid").getElementsByTagName("input");
for(var i = 0; i < options.lenght; ++i)
{
if(options[i].type == "checkbox")
{
options[i].onclick = checkboxclick;
}
}
}

function checkboxclick()
{
alert("hh");
} --------------------编程问答--------------------
引用 2 楼 koukoujiayi 的回复:
在DataBound事件中进行:
    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            CheckBoxList1.Items[i].Attributes.Add("onclick", "jsFunc('"+i.ToString()+"')");
    }


UP --------------------编程问答-------------------- 用jQuery绑定多好啊
$(".aaa").click(test); --------------------编程问答--------------------
引用 6 楼 zhujiazhi 的回复:
funtion addclick()
{
var options = document.getElementById("checkboxlistid").getElementsByTagName("input");
for(var i = 0; i < options.lenght; ++i)
{
if(options[i].type == "checkbox")
{
options[i].onclick = checkboxclick;
}
}
}

function checkboxclick()
{
alert("hh");
}


有没有别的方法在后台添加呢?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,