Results 1 to 10 of 10

Thread: [2005] How to determine which html checkboxes are checked?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    [2005] How to determine which html checkboxes are checked?

    I have an ASP page that generates a dynamic list at run time which contains an HTML (not ASP) Checkbox next to each item. My question is, once a PostBack occurs, how can I loop through all the Checkboxes with a given name such as:

    Code:
            <input id="clients" type="checkbox" value="1" />
            <input id="clients" type="checkbox" value="2" />
            <input id="clients" type="checkbox" value="3" />
    In the code behind, I need to be able to loop through all Checkboxes with the ID of "clients" and determine the "value" and the "checked" property.

    I don't want to use the checkboxlist control or datagrid, so please don't suggest that. I really need the above to work.

    Any example would be appreciated.

    Visual Studio 2010

  2. #2
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: [2005] How to determine which html checkboxes are checked?

    Hi, I´m no expert, but have you tried adding the runat="server" attribute to each control?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [2005] How to determine which html checkboxes are checked?

    Quote Originally Posted by Rauland
    Hi, I´m no expert, but have you tried adding the runat="server" attribute to each control?
    Hmmm... Didn't know you could do that with HTML controls. But, will this work if that code is generated by a response.write in the code behind on page load? I thought "runat", only works if it was statically created on the html page.

    I'm not at my development computer now, but assuming this does work, I would still have to loop through the controls (unknown number at run time) an determine if it is checked. I don't know how to do this. Can someone post a quick example of iterating through each html checkbox in the codebehind in VB?

    Visual Studio 2010

  4. #4
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: [2005] How to determine which html checkboxes are checked?

    Hi,
    I´m not sure whether this will help or not:

    But have been doing a couple of tests.
    Here´s the aspx page:
    Supposing there are three radio buttons. Which belong to a group called, group1.

    Also the result is shown in a literal control. I know you were asking for vb.net code! apologies for just posting in c# !

    Code:
    <body>
        <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                     Radio 1:<input id="Radio1" runat="server"  name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                    <td>
                     Radio 2:<input id="Radio2" runat="server" name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                    <td>
                      Radio 3:<input id="Radio3" runat="server" name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                <td>
                <asp:Literal runat="server" ID="testId"></asp:Literal>
                </td>
                </tr>
            </table>
            <input id="Submit1" type="submit" value="submit" />
        </div>
        </form>
    </body>
    And here´s the code behind code, so far in c#.

    Code:
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            foreach (Control ctr in this.form1.Controls)
            {
                if (ctr is HtmlInputRadioButton)
                {
                    HtmlInputRadioButton rdBtn = ctr as HtmlInputRadioButton;
                    if (rdBtn.Name == "group1" && rdBtn.Checked==true)
                    {
                       testId.Text = string.Format("Radio button with <b>id:{0}</b> from radio button <b>group{1}</b> checked", rdBtn.ID, rdBtn.Name);
                      break;
                   }
                }
            }
    
        }
    }

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] How to determine which html checkboxes are checked?

    Looping through them is right and all, but you could try something... because those are controls that have been created and rendered onto the page, when your event occurs in which you want to detect which checkboxes have been selected, place a breakpoint anywhere and have a look inside Request.Form. I say this because a postback is nothing but an HTTP Post and you should therefore be able to see which checkboxes were selected.

    Failing that, you could just use a checkboxlist and bind it to your 'list'.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [2005] How to determine which html checkboxes are checked?

    Quote Originally Posted by Rauland
    Hi,
    I´m not sure whether this will help or not:

    But have been doing a couple of tests.
    Here´s the aspx page:
    Supposing there are three radio buttons. Which belong to a group called, group1.

    Also the result is shown in a literal control. I know you were asking for vb.net code! apologies for just posting in c# !

    Code:
    <body>
        <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                     Radio 1:<input id="Radio1" runat="server"  name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                    <td>
                     Radio 2:<input id="Radio2" runat="server" name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                    <td>
                      Radio 3:<input id="Radio3" runat="server" name="group1" type="radio" />
                    </td>
                </tr>
                <tr>
                <td>
                <asp:Literal runat="server" ID="testId"></asp:Literal>
                </td>
                </tr>
            </table>
            <input id="Submit1" type="submit" value="submit" />
        </div>
        </form>
    </body>
    And here´s the code behind code, so far in c#.

    Code:
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            foreach (Control ctr in this.form1.Controls)
            {
                if (ctr is HtmlInputRadioButton)
                {
                    HtmlInputRadioButton rdBtn = ctr as HtmlInputRadioButton;
                    if (rdBtn.Name == "group1" && rdBtn.Checked==true)
                    {
                       testId.Text = string.Format("Radio button with <b>id:{0}</b> from radio button <b>group{1}</b> checked", rdBtn.ID, rdBtn.Name);
                      break;
                   }
                }
            }
    
        }
    }
    This might work. I'll give it a shot when I get home. Thanks!

    Visual Studio 2010

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [2005] How to determine which html checkboxes are checked?

    Quote Originally Posted by mendhak
    Looping through them is right and all, but you could try something... because those are controls that have been created and rendered onto the page, when your event occurs in which you want to detect which checkboxes have been selected, place a breakpoint anywhere and have a look inside Request.Form. I say this because a postback is nothing but an HTTP Post and you should therefore be able to see which checkboxes were selected.

    Failing that, you could just use a checkboxlist and bind it to your 'list'.
    But how flexible is a CheckboxList? Can it be embedded in a table like:

    Code:
    <table>
       <tr>
          <td>Checkbox1</td><td>Name</td>
          <td>Checkbox2</td><td>Name</td>
          <td>Checkbox3</td><td>Name</td>
          <td>Checkbox4</td><td>Name</td>
       </tr>
    </table>

    Visual Studio 2010

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] How to determine which html checkboxes are checked?


  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [2005] How to determine which html checkboxes are checked?

    Hmmm.. so how does it now what table to display in? I don't see that part in the example code.

    Visual Studio 2010

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] How to determine which html checkboxes are checked?

    What table to display in? You place that control in whichever cell it should show up in, and bind it to your data. It then creates the 'rows' itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width