Solved

Checkbox Onclick aspx

Posted on 2008-10-15
4
807 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now