views:

342

answers:

4

Hi

I have a ASP.NET web page which contains a table of check boxes and a button. I need to find the IDs of all the check boxes which are checked when the button click happens. Once the list of IDs are collected, it needs to be passed on to the server. I am able to do it using jQuery and PageMethods.

How can I achieve this in the button click handler in the code behind file? i.e. the IDs of all the check boxes which are checked when button click happens.

I want to do it in code behind because the code behind file contains a few functions which create a chart and export it to PDF and the chart gets created based on the checked check boxes.

Please suggest.

cheers

+2  A: 

If your checkboxes are server controls then then just just iterate through the checkbox list items and find which are checked in your Button click event.

If your checkboxes are html controls then then use put the values into a hidden html input control using javascript then access this value in Button click event.

Binoj Antony
Pulse has illustrated how to do this as well!
Binoj Antony
+1  A: 

First, I would recommend that you reconsider why you're trying to discover which checkboxes are selected on the client side and pass that as an argument to the form post. Why not enumerate them in the button's onclick event directly?

Second, assuming there's some ultimate requirement to do so, I'd recommend you use jquery to discover all checked boxes and store their IDs in a delimited string in a hidden form field. I'm not sure why you feel compelled to pass this information as a direct argument to the button click event itself, but it's not advisable to do so.

Edit: an ASP.NET button control has a CommandArgument property you could use to store this, but it does not appear this is settable from the client side.

Chris
+1  A: 

Since you have collected all the ids uing jQuery it would be better to push all the ids into an array and then join the array using a concatenation operator like ';'. Save this concatenated value to a hidden text field <input type='hidden' id='txtHidIds' /> with runat="server" attribute set. Then get the value of this hidden field from C# using

txtHidIds.Value

and then split using the concatenation operator used. You can get the result in an array and then manipulate the array containing all the ids.

Something like

$("#btn1").click(function() {
    var arrChecked = new Array();

    $("input[type='checkbox'].childCheck:checked").each(function() {
        arrChecked.push($(this).attr("id"));
    });

    $("#txtHidIds").val(arrChecked.join(';'));
});
rahul
+1  A: 

An alternative to the previous answers that can be used if you are not using server controls for your checkboxes or the checkboxes have been added dynamically at runtime is to iterate through the Form collection sent as part of the post back. If a check box has been checked it will be added to this collection with its id as its key and a value of "on". If it is not checked it will not be added to the collection. To use this method you will need to ensure some kind on naming convention to allow you to identify the checkbox ids you are after:

    foreach (var key in Request.Form.AllKeys)
    {
        if (key.EndsWith("checkbox", true, null))
        {
            // If entry found then this checkbox has been checked
        }
    }
Andy Rose