-
Aug 25th, 2009, 09:00 AM
#1
adding OnClick event to checkbox list
i have a checkbox list in my form and i populate it like this
Code:
Using conn As New SqlConnection(Methods.ConnectionString())
Using Command As New SqlCommand("SELECT A.[id],A.[description] " & _
" FROM catagory A" & _
" ORDER BY A.[description]", conn)
conn.Open()
cbl_catagoryList.DataSource = Command.ExecuteReader(CommandBehavior.CloseConnection)
cbl_catagoryList.DataTextField = "description"
cbl_catagoryList.DataValueField = "id"
cbl_catagoryList.DataBind()
End Using
End Using
i need each check box ( input type = "checkbox") will have onclick event.
how do i do that ?
* Rate It If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID
-
Aug 25th, 2009, 09:11 AM
#2
Re: adding OnClick event to checkbox list
Hey,
Once the data binding is completed, you will need to loop through the Items collection of the CheckBoxList:
http://msdn.microsoft.com/en-us/libr...rol.items.aspx
And then set the onclick event.
Gary
-
Aug 25th, 2009, 11:55 AM
#3
Re: adding OnClick event to checkbox list
Checkboxlist has a DataBinding event too. Looping through is probably better.
vb Code:
For Each li As ListItem in cbl_cat[B]e[/B]goryList li.Attribute.Add("onclick","alert('omg roflcopterz');") Next
-
Aug 25th, 2009, 12:50 PM
#4
Re: adding OnClick event to checkbox list
can i ask why looping is better then adding its attribute in the data binding ?
adding the att when the checkbox is created seems more "natural" to me
* Rate It If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID
-
Aug 25th, 2009, 01:06 PM
#5
Re: adding OnClick event to checkbox list
another question, how do i get to the text of the checkbox (the one inside a label) with javascript ?
* Rate It If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID
-
Aug 25th, 2009, 01:20 PM
#6
Re: adding OnClick event to checkbox list
ok i got the text changed, i'll post the js function to here if someone ever need an example:
Code:
function selectedJobs(meObjectID) {
var counter = 0
var objectID = document.getElementById('<%=cbl_catagoryList.ClientID%>');
var labelArray = objectID.getElementsByTagName("label");
var checkBoxArray = objectID.getElementsByTagName('input');
var checkedValues = '';
for (k = 0;k< checkBoxArray.length; k++) {
var checkBoxRef = checkBoxArray[k];
if (checkBoxRef.checked) {
counter++
labelArray[k].style.color = 'red'
}
}
if (parseInt(counter) > 5) {
meObjectID.checked = false;
counter = 0
}
document.getElementById("JobsLeft").innerHTML = 5 - counter;
}
* Rate It If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID
-
Aug 26th, 2009, 01:27 AM
#7
Re: adding OnClick event to checkbox list
Originally Posted by motil
can i ask why looping is better then adding its attribute in the data binding ?
adding the att when the checkbox is created seems more "natural" to me
As with any databound control (repeater, *list, grid), the DataBound event is the proper way to do it, but in the case of a checkboxlist where it's a simlpe list and all you want to do is add an onclick, it's easier to do it by looping. I feel a little caught out here.
-
Aug 26th, 2009, 02:12 AM
#8
Re: adding OnClick event to checkbox list
Originally Posted by mendhak
it's easier to do it by looping. I feel a little caught out here.
I am shocked!!!
-
Aug 30th, 2009, 02:18 PM
#9
Fanatic Member
Re: adding OnClick event to checkbox list
Originally Posted by gep13
Originally Posted by mendhak
I feel a little caught out here.
I am shocked!!!
As one of other posters would say "I'm lol'in out".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|