473,469 Members | 1,662 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

OnClick Attribute in CheckBoxList

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

}
Nov 19 '05 #1
2 2066
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" wrote:
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');");

}

Nov 19 '05 #2
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" wrote:
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" wrote:
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');");

}

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected...
2
by: Sedef | last post by:
Hi, i'm trying to create a custom Button user control which will be derived from System.Web.UI.WebControls.Button. the normal server side Button class creates some client side javascript code for...
4
by: Shaul Feldman | last post by:
Hello, I have something really awkward at work - fighting with CheckBoxList... How can I define CSS for ListItem in CheckBoxList programmatically. I add CheckBoxList's Items on the fly, something...
3
by: ADavidson | last post by:
I'm getting a {"Parser Error: The Runat attribute must have the value Server." } error when I try to get the Server.GetlastError() in the Global.asax codebehind. Why am I getting this? I...
3
by: vikram | last post by:
How can i add a user define attribute to each checkbox item in a checkbox list control. I want to add a attribute "Tag". Is it possible
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
3
by: Roy | last post by:
The title is self-explanatory, but I'll show you all what I've done thus far. Here's the aspx side of things(trimmed, of course): <form name="form1" id="Form1" method="post" runat="server">...
0
by: ismailc | last post by:
Hi, this is related to my previous post. Don't know xsl at all, but I'm trying to create 2 btns instead of a 2 radio buttons with one btn. Please help in building a condition in setting the ID,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.