CheckBox web controls using a loop

Discussion in 'ASP .Net Web Controls' started by bebop, Feb 20, 2004.

  1. bebop

    bebop Guest

    Individual CheckBox Web Controls ..HELP
    I'm using three checkbox web controls in C# .NET, one button, and one labe

    Is there a way to "group" these individual checkbox web controls

    If so, how can I iterate the checkboxes using a loop, array, etc

    What I'm looking for is how to determine what checkbox was selected from the three checkbox web controls and have the selected checkbox value(s) displayed in a label

    I don't want to use the following

    if (CheckBox1.Checked == true

    //do something

    if (CheckBox2.Checked == true

    //do somethin

    and so fort

    What if I had 50 Individual Checkbox web controls I don't want to have the above if statement 50 times in my code behind

    I want to have something else-such as a loop that will iterate through the Individual Checkbox controls to check which is selected and print the selected checkbox value out to the label

    Can this be done without using CheckBoxList web control

    Any suggestions would be appreciated

    Thanks

    bebo

    code

    <asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 32px" runat="server
    Text="aaa"></asp:CheckBox><asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 64px" runat="server
    Text="bbb"></asp:CheckBox><asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 96px" runat="server
    Text="ccc"></asp:CheckBox><asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 144px" runat="server
    Text="Button"></asp:Button><asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 48px" runat="server"></asp:Label

    ******
     
    bebop, Feb 20, 2004
    #1
    1. Advertising

  2. bebop

    Jos Guest

    "bebop" <> wrote in message
    news:...
    > Individual CheckBox Web Controls ..HELP
    > I'm using three checkbox web controls in C# .NET, one button, and one

    label
    >
    > Is there a way to "group" these individual checkbox web controls?
    >
    > If so, how can I iterate the checkboxes using a loop, array, etc.
    >
    > What I'm looking for is how to determine what checkbox was selected from

    the three checkbox web controls and have the selected checkbox value(s)
    displayed in a label.
    >
    > I don't want to use the following:
    >
    > if (CheckBox1.Checked == true)
    > {
    > //do something
    > }
    > if (CheckBox2.Checked == true)
    > {
    > //do something
    > }
    > and so forth
    >
    > What if I had 50 Individual Checkbox web controls I don't want to have the

    above if statement 50 times in my code behind;
    >
    > I want to have something else-such as a loop that will iterate through the

    Individual Checkbox controls to check which is selected and print the
    selected checkbox value out to the label.
    >
    > Can this be done without using CheckBoxList web control?


    If you want a loop, you can group your checkboxes together in a panel,
    and then use something like this:

    foreach (Control c in Panel1.Controls) {
    if(c is CheckBox) {
    CheckBox cb = (CheckBox)c;
    // do something with cb
    }
    }

    Another solution would be to set the OnCheckedChanged event of
    each checkbox to the same event handler, and perform the action there.
    In this case however, the code will only be executed for checkboxes
    where the selection has been changed by the user.
    If they were all off to begin with, maybe this is what you want.

    The CheckBoxList is by far the easiest solution however.

    --

    Jos
     
    Jos, Feb 20, 2004
    #2
    1. Advertising

  3. bebop

    cw bebop Guest

    Hi Jos

    Your response is close to what I'm looking for.

    This is what I tried:

    HTML code:

    <form id="Form1" method="post" runat="server">
    <asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
    absolute; TOP: 32px" runat="server"
    Text="aaa"></asp:CheckBox>
    <asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 48px; POSITION:
    absolute; TOP: 64px" runat="server"
    Text="bbb"></asp:CheckBox>
    <asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 48px; POSITION:
    absolute; TOP: 96px" runat="server"
    Text="ccc"></asp:CheckBox>
    <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 48px; POSITION:
    absolute; TOP: 128px" runat="server"
    Text="Button"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 160px; POSITION:
    absolute; TOP: 40px" runat="server"></asp:Label>
    </form>
    ************

    C# code behind:

    private void Button1_Click(object sender, System.EventArgs e)
    {
    CheckBox cb;
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach(Control c in Page.Controls[1].Controls)
    {
    if (c is CheckBox)
    {
    cb = (CheckBox)c;
    CheckBox cb = (CheckBox)c;
    //sb.Append(cb.ID + " : " + cb.Checked.ToString() + " : " + cb.Text +
    "<br>");
    sb.Append(cb.Checked.ToString());

    }
    }
    Label1.Text = sb.ToString();

    }

    **********
    When running the web app I checked the first checkBox and get the
    result: TrueFalseFalse

    Which is correct except that I want the value of the checkBox displayed
    in Label1

    Any ideas?

    bebop





    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    cw bebop, Feb 22, 2004
    #3
  4. bebop

    Jos Guest

    "cw bebop" <> wrote in message
    news:%23wxkJxN%...
    >
    > Hi Jos
    >
    > Your response is close to what I'm looking for.
    >
    > This is what I tried:
    >
    > HTML code:
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:CheckBox id="CheckBox1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
    > absolute; TOP: 32px" runat="server"
    > Text="aaa"></asp:CheckBox>
    > <asp:CheckBox id="CheckBox2" style="Z-INDEX: 102; LEFT: 48px; POSITION:
    > absolute; TOP: 64px" runat="server"
    > Text="bbb"></asp:CheckBox>
    > <asp:CheckBox id="CheckBox3" style="Z-INDEX: 103; LEFT: 48px; POSITION:
    > absolute; TOP: 96px" runat="server"
    > Text="ccc"></asp:CheckBox>
    > <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 48px; POSITION:
    > absolute; TOP: 128px" runat="server"
    > Text="Button"></asp:Button>
    > <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 160px; POSITION:
    > absolute; TOP: 40px" runat="server"></asp:Label>
    > </form>
    > ************
    >
    > C# code behind:
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > CheckBox cb;
    > System.Text.StringBuilder sb = new System.Text.StringBuilder();
    > foreach(Control c in Page.Controls[1].Controls)
    > {
    > if (c is CheckBox)
    > {
    > cb = (CheckBox)c;
    > CheckBox cb = (CheckBox)c;
    > //sb.Append(cb.ID + " : " + cb.Checked.ToString() + " : " + cb.Text +
    > "<br>");
    > sb.Append(cb.Checked.ToString());
    >
    > }
    > }
    > Label1.Text = sb.ToString();
    >
    > }
    >
    > **********
    > When running the web app I checked the first checkBox and get the
    > result: TrueFalseFalse
    >
    > Which is correct except that I want the value of the checkBox displayed
    > in Label1
    >
    > Any ideas?
    >
    > bebop


    I guess you want to know which checkboxes are checked?

    Instead of
    > sb.Append(cb.Checked.ToString());


    Try:
    if (cb.Checked) sb.Append(cb.ID);

    --

    Jos
     
    Jos, Feb 22, 2004
    #4
  5. bebop

    cw bebop Guest

    I did the following:

    C# code behind:

    private void Button1_Click(object sender, System.EventArgs e)
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach(Control c in Page.Controls[1].Controls)
    {
    if (c is CheckBox)
    {
    CheckBox cb = (CheckBox)c;

    if (cb.Checked) sb.Append(cb.ID);
    }
    }
    Label1.Text = sb.ToString();
    }
    ******

    It does work when displaying the selected CheckBox control except that
    it didn't display the CheckBox's text value.

    I changed where it reads

    if (cb.Checked) sb.Append(cb.ID)

    TO

    if (cb.Checked) sb.Append(cb.Text)

    That did the trick. It works. I wanted to display the text value using
    CheckBox web Controls instead of CheckBoxList.

    I have question..

    If you look at the code behind where the if statement is:

    {
    if (c is CheckBox)
    {
    CheckBox cb = (CheckBox)c;

    if (cb.Checked) sb.Append(cb.ID);
    }

    I'm wondering will the nested if statement cause any problems or
    exceptions later on?

    It looks strange that I can put an if statement within an if statement.
    I'm not used to seeing this.

    Does this code look all right and won't cause problems later on (ie.
    exception is thrown)

    What do you think?

    bebop




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    cw bebop, Feb 22, 2004
    #5
  6. bebop

    Jos Guest

    cw bebop wrote:
    > I have question..
    >
    > If you look at the code behind where the if statement is:
    >
    > {
    > if (c is CheckBox)
    > {
    > CheckBox cb = (CheckBox)c;
    >
    > if (cb.Checked) sb.Append(cb.ID);
    > }
    >
    > I'm wondering will the nested if statement cause any problems or
    > exceptions later on?
    >
    > It looks strange that I can put an if statement within an if
    > statement. I'm not used to seeing this.
    >
    > Does this code look all right and won't cause problems later on (ie.
    > exception is thrown)


    No, no problem at all.
    Nested if's are quite common.
    Actually, one of the purposes of exceptions is to catch the
    exception, no matter how many levels of loops or conditions.

    --

    Jos
     
    Jos, Feb 22, 2004
    #6
  7. bebop

    cw bebop Guest

    Thank you Jos for your help.

    Problem: to see if Individual CheckBox web controls could be grouped and
    whichever is selected displayed in a Label.

    Solved. Thanks again.

    bebop




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    cw bebop, Feb 22, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    5
    Views:
    1,082
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  2. Nathan Sokalski
    Replies:
    4
    Views:
    340
    Nathan Sokalski
    Dec 21, 2006
  3. Nathan Sokalski
    Replies:
    4
    Views:
    378
    Nathan Sokalski
    Dec 21, 2006
  4. Raheel Sharif
    Replies:
    0
    Views:
    164
    Raheel Sharif
    Sep 27, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page