Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi experts,

am working on asp.net c#

on my webpage i have 10 checkboxes.....not checkboxlist.

and a label

every checkbox has its name and id

for example checkbox1 it has id as chk1 and text as 1
and
checkbox2 it has id as chk2 and text as 2 and so on....... till checkbox 10

when user select 5 checkboxes...in label it must show (lblSelectedChkbxs) as from 1 To 5

How can we do this using sql query ??

Please can you help me.

Thanks in advance.
Posted
Comments
Praveen Kumar Upadhyay 8-Jan-15 1:22am    
What you want to display on label?
Member239258 8-Jan-15 1:32am    
Sir, I want to display selected checkbox value

It should display as 1-5 like this in label.

so here checkboxes from 1 to checkbox5 are selected.

Please help.
Thanks.
Praveen Kumar Upadhyay 8-Jan-15 1:48am    
Let's say user has selected chk1,chk2,chk3,chk4 and chk5 then you want to display the 1st chk1.value and last selected chk.value. Am I right?
Member239258 8-Jan-15 2:02am    
yes, you are right.

Please help.
Praveen Kumar Upadhyay 8-Jan-15 2:06am    
tell me one thing. What if user select chk1,chk2,chk6 then what to display on label.

1 solution

Please find the code block below.
XML
<asp:Button Text="Click Me" ID="btn" runat="server" OnClick="btn_Click" />
            <asp:Panel ID="Panel1" runat="server">
            <asp:CheckBox Text="1" runat="server" ID="chk1" />
            <asp:CheckBox Text="2" runat="server" ID="chk2" />
            <asp:CheckBox Text="3" runat="server" ID="chk3" />
            <asp:CheckBox Text="4" runat="server" ID="chk4" />
            <asp:CheckBox Text="5" runat="server" ID="chk5" />
            <asp:CheckBox Text="6" runat="server" ID="chk6" />
            <asp:CheckBox Text="7" runat="server" ID="chk7" />
            <asp:CheckBox Text="8" runat="server" ID="chk8" />
            <asp:CheckBox Text="9" runat="server" ID="chk9" />
            <asp:CheckBox Text="10" runat="server" ID="chk10" />
            </asp:Panel>
            <asp:Label Text="" ID="lbl" runat="server" />


Below is the code behind

C#
protected void btn_Click(object sender, EventArgs e)
        {
            List<int> values = new List<int>();
            foreach (Control item in Panel1.Controls)
            {
                if (item.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
                {
                    CheckBox c = (CheckBox)item;
                    if (c.Checked)
                        values.Add(Convert.ToInt16(c.Text));
                }
            }

            if (values.Count > 0)
            {
                values.Sort();
                lbl.Text = values[0] + " - " + values[values.Count - 1];
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900