Solved

Checkbox Onclick aspx

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

856 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