views:

180

answers:

2

Hello,

I have a listview bound to a datasource, each row has a checkbox control on it, what i want to acheive is when i press a button, i want to check through all of the checkboxes in the listview and check if any of them have been ticked.

What i tried first was to add a onclick event to a button and loop around the listview.items and check the "checked" state of the checkbox control, however, this always returned "false" even if they have ticked the item.

The only way i could get it to work is if i looped around all the listview items inside the onItemDataBound event, however this doesnt seem the most efficient way of doing it as it will call onItemDataaBound for every item in the listview, (thus called foreach on the listview.items each time)

I then tried doing the same on the OnDateBound event instead as this is only called once, but i ran into the same problem, it sees the checkboxes "checked" field as false all the time, i was wondering if anyone could point me into the right direction of the proper way to do it.

thanks Raj.

A: 

I use a gridview that has the same functionality. I put a button outside of the gridview with an onclick event.

In the code behind I have something like this:

foreach (GridViewRow gridViewRow in GridAvailableUsers.Rows)
            {
                // only add selected topics.
                CheckBox selCheckBox = (CheckBox)gridViewRow.Cells[3].Controls[1];
                if (selCheckBox.Checked)
                {
// do something
                }
            }

This will loop through all the rows. If you don't want something like that, then you may have to go with a javascript solution.Capture all the rows with the checkbox checked and update a hidden field with the row id's

krishna
A: 

Hi Raj,

There must be DataBind() function called on your listview before your Button_Click event; most likely to be in the page_load event. If you've bound your data in page_load event; make it inside if (!Page.IsPostBack) block;

Hope this will help.

Regards, Tauqeer.....

Tauqeer