adding OnClick event to checkbox list-VBForums
Results 1 to 9 of 9

Thread: adding OnClick event to checkbox list

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,128

    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

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    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

  3. #3
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: adding OnClick event to checkbox list

    Checkboxlist has a DataBinding event too. Looping through is probably better.

    vb Code:
    1. For Each li As ListItem in cbl_cat[B]e[/B]goryList
    2. li.Attribute.Add("onclick","alert('omg roflcopterz');")
    3. Next

  4. #4

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,128

    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

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,128

    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

  6. #6

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,128

    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('<&#37;=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

  7. #7
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: adding OnClick event to checkbox list

    Quote Originally Posted by motil View Post
    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.

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: adding OnClick event to checkbox list

    Quote Originally Posted by mendhak View Post
    it's easier to do it by looping. I feel a little caught out here.
    I am shocked!!!

  9. #9
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: adding OnClick event to checkbox list

    Quote Originally Posted by gep13 View Post
    Quote 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.