?
Solved

Checkbox Onclick aspx

Posted on 2008-10-15
4
Medium Priority
?
832 Views
Last Modified: 2012-05-05
Hi experts

I have a checkbox populating from a database

<asp:CheckBoxList ID ="lstStudents" runat="server" CssClass="nowrapping"  DataValueField="PersonId"  DataTextField="DisplayName"  SelectionMode="Multiple" onclick="redir(this)"   ></asp:CheckBoxList>

and  my javascript


function redir(sel){

alert(sel.Value);

}


The result is undefined.

How do I get it  to display the value?

Thanks a lot experts
Nik
0
Comment
Question by:nikdonovanau
4 Comments
 
LVL 7

Expert Comment

by:maXXXeE
ID: 22728676
hi
Please try with
OnClientClick="javascript:redir(this);
(or)
OnClientClick="redir(this)"

instead of
onclick="redir(this)"
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 2000 total points
ID: 22735008
Checkboxes on the client don't have values. They have the checked property.
Since ASP.NET is having a so called 'CheckBoxList' control, you will only get the required value server side.
It doesn't mean you can use that logic clientside.

Look in the source of your browser. Each checkbox has a unique id and each checkbox is an individual element (control) in the browser.
0
 
LVL 1

Author Closing Comment

by:nikdonovanau
ID: 31506586
Thanks for that, confirmed my fear so I rewrote the page a different way using hyperlinks and javascript instead.

I managed to output the id of the checkbox but that didnt give me enough info to do what I was after.

Cheers
0
 
LVL 13

Expert Comment

by:sm394
ID: 22736920
Try that

    <script type="text/javascript" language="javascript">

                function getVal() {
            var vals = document.getElementById('div1').getElementsByTagName("label");
            var inputs = document.getElementById('<%=chbListTest.ClientID %>').getElementsByTagName("input");


            for (var i = 0; i < vals.length; i++) {
                if(inputs[i].checked)
                  alert(vals[i].innerText);
            }
        }
    </script>

<button  onclick="getVal()">Get Selected Vals</button>
    <div id="div1">
             <asp:CheckBoxList runat="server" ID="chbListTest">
            <asp:ListItem Text="1" ></asp:ListItem>
            <asp:ListItem Text="2" ></asp:ListItem>
            <asp:ListItem Text="3" ></asp:ListItem>
        </asp:CheckBoxList>
    </div>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question