Monday, 5 July 2021

How To Get Selected Values Of CheckBoxList With Foreach/LINQ in ASP.NET C#?

7/05/2021 - By Pranav Singh 0

In this article I will show you how you can get the selected value of your checkboxlist in your asp.net application using c#.net. in this I will use LINQ and for look to get the selected checkboxlist value your can get.   

How To Get Selected Values Of CheckBoxList With Foreach/LINQ in ASP.NET C#?


So first we will take a checkboxlist control and bind some value. 


     <asp:CheckBoxList ID="cblItemType" runat="server" RepeatDirection="Horizontal">

                <asp:ListItem>Item1</asp:ListItem>

                <asp:ListItem>Item2</asp:ListItem>

                <asp:ListItem>Item3</asp:ListItem>

            </asp:CheckBoxList>

 

Now here are the code:

With LINQ to get selected item value:

     List<string> selectedItem = cblItemType.Items.Cast<ListItem>()

                                                    .Where(li => li.Selected)

                                                    .Select(li => li.Value)

                                                    .ToList();

 

With Foreach:

List<string> selectedItem = new List<string>();

                    foreach(ListItem item in cblItemType.Items)

                    {

                        if (item.Selected)

                        {

                               selectedItem.Add(item.Value);

                        }

                    }

 

Now run the article to check the output.

 

Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top