OnClick Attribute in CheckBoxList

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');");

}
 
G

Guest

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
 
G

Guest

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');");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top