OnClick Attribute in CheckBoxList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi.

I Have a CheckBoxList and I would like the add the onclick attribute. But
when I add the attribute only the first one in my list get the onclick
attribute.

Here is my code.

protected CheckBoxList myCheckBoxList;

private void Page_Load(object sender, System.EventArgs e)
{
// Binnds myc CheckBoxlList with human with Name and Age
myCheckBoxList.DataSource = al;
myCheckBoxList.DataTextField = "Name";
myCheckBoxList.DataValueField = "Age";
myCheckBoxList.DataBind();
}
protected void myCheckBoxList_OnDataBinding(object sender, EventArgs e)
{
CheckBoxList chkList = (CheckBoxList)sender;
CheckBox cBox = (CheckBox)chkList.Controls[0];
cBox.Attributes.Add("onclick","alert('hello world');");

}
 
Hi Nemo,
as visible from your code you are always trying to add onclick attribute to
the first control in the checkbox list so you are getting the event on the
first checkbox in the list.
instead u can add the onclick attribute to the checkboxlist itself instead
of adding it to the checkbox.If you have further doubts please get back
hth
srini
 
Hi.

But if I want to do something like this your theri does'nt work.
cBox.Attributes.Add("onclick","alert(this.id);");

One unique alert from every CheckBox.

srini said:
Hi Nemo,
as visible from your code you are always trying to add onclick attribute to
the first control in the checkbox list so you are getting the event on the
first checkbox in the list.
instead u can add the onclick attribute to the checkboxlist itself instead
of adding it to the checkbox.If you have further doubts please get back
hth
srini

Nemo said:
Hi.

I Have a CheckBoxList and I would like the add the onclick attribute. But
when I add the attribute only the first one in my list get the onclick
attribute.

Here is my code.

protected CheckBoxList myCheckBoxList;

private void Page_Load(object sender, System.EventArgs e)
{
// Binnds myc CheckBoxlList with human with Name and Age
myCheckBoxList.DataSource = al;
myCheckBoxList.DataTextField = "Name";
myCheckBoxList.DataValueField = "Age";
myCheckBoxList.DataBind();
}
protected void myCheckBoxList_OnDataBinding(object sender, EventArgs e)
{
CheckBoxList chkList = (CheckBoxList)sender;
CheckBox cBox = (CheckBox)chkList.Controls[0];
cBox.Attributes.Add("onclick","alert('hello world');");

}
 
Back
Top