How to use LINQ to get selected items (checkboxes) within a GridView control that allow paging?

I'm exploring the possibility to use LINQ to get selected items (checkboxes) within a GridView control that allow paging.


Checkbox is added like this:

<asp:TemplateField>
                      <ItemTemplate>
                      <asp:CheckBox ID="chk" runat="server" />
                      </ItemTemplate>
                      </asp:TemplateField>


Anyone can advise here? Thanks
LVL 57
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abelCommented:
It seems that you want to query all checkboxes within a GridView, from outside the GridView (i.e., not from within any updating event of the checkbox_change event).

Reading your request, I though it wasn't trivial, but it isn't that hard, really. This will give you what you want:

var checkedCheckBoxes = from GridViewRow row in GridView1.Rows 
                        where (row.FindControl("chk") as CheckBox).Checked
                        select row.FindControl("chk");
 
foreach(CheckBox chk in checkedCheckBoxes)
{
    Debug.WriteLine("Text is: " + chk.Text);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abelCommented:
(PS: there's also a LINQ zone, in case you want to make any follow-up questions....;-).
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerAuthor Commented:
Hi there,

Thanks for your codes, have some fun with it, and my final outcome is something like below:

var checkedIDs = from GridViewRow row in GridView1.Rows
                            where (row.FindControl("chk") as CheckBox).Checked
                            select (row.FindControl("id") as HiddenField).Value;


//....

cheers
0
abelCommented:
Looks good. Tx for the grade! ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.