1

ASP.NET:

<asp:Panel ID="pnlFilter" runat="server">
    <div class="dvFilter">
        <input type="checkbox" id="cb01" checked="checked" />
        <label for="cb01">All</label>
    </div>
    <div class="dvFilter">
        <input type="checkbox" id="cb02" checked="checked" />
        <label for="cb02">None</label>
    </div>
</asp:Panel>

C#:

foreach (Control item in this.form1.Controls)
{
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = item as System.Web.UI.HtmlControls.HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }

}

I am getting a null value for the _cbx variable.

How can I update it so I am able to get the ID of all the checked input type checkboxes.

I tried this answer: Count the number of TextBoxes and CheckBoxes in a form but didn't work for me either.

Si8
  • 9,141
  • 22
  • 109
  • 221
  • You should look into `runat=server` – VDWWD Nov 08 '17 at 15:01
  • Tried that already but let me "retry" it again :) – Si8 Nov 08 '17 at 15:05
  • I shouldn't have to use runat="server" because I am looking for HtmlInputCheckBox... unless I am wrong? – Si8 Nov 08 '17 at 15:07
  • None of solution worked except Lokki, however there were some limitation. I will +1 but won't accept the answer as it didn't completely resolve my issue. I manually checked for each CheckBox... for now. I will post a solution if I come across or can figure out what the issue is. Thanks. – Si8 Nov 08 '17 at 16:56
  • this could help [link](https://forums.asp.net/t/1929531.aspx?How+to+Count+Checked+CheckBoxes+in+the+page+) ,and this [link](https://stackoverflow.com/questions/10460378/how-to-get-selected-items-count-in-aspcheckboxlist) is if you want to do it in a checkbox list way hope it helps – Jephren Naicker Nov 08 '17 at 21:27

7 Answers7

2

You need to parse the DOM if you want to use <input type="checkbox">.

I advise you to use <asp:CheckBox> instead of <input type="checkbox">. Then you can access your controls from c#

In your lookup:
var _cbx = item as System.Web.UI.WebControls.CheckBox;

You can get all checkboxes without looping through all controls.
Use LINQ:
this.form1.Controls.Where(c=>c.GetType() == typeof(CheckBox))

Lokki
  • 372
  • 1
  • 6
  • 20
1

I believe the problem is that your checkboxes do not run at server. As such they have no control "assigned" in the code-behind. Try adding the attribute runat="server" to your checkboxes. Just like you have in your panel.

CubanTurin
  • 177
  • 1
  • 10
1

Use this:

foreach (HtmlElement element in webBrowser1.Document.All)
{
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = element as System.Web.UI.HtmlControls.HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }
}
Shuvra
  • 199
  • 11
  • `webBrowser1`? What's that? – Si8 Nov 08 '17 at 16:11
  • The code example requires that your application contains a WebBrowser control named WebBrowser1. Find the example in MSDN help: https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.all(v=vs.110).aspx – Shuvra Nov 08 '17 at 16:18
  • I don't unfortunately. – Si8 Nov 08 '17 at 16:22
  • What is your page name?? change webBrowser with ur page name . It should work. – Shuvra Nov 08 '17 at 16:26
1

First of all, you need to add runat="server" or change them to asp:CheckBox controls.

But the reason you can't find them if you add them is because they are in another control. So look for them in pnlFilter.

<asp:Panel ID="pnlFilter" runat="server">
    <div class="dvFilter">
        <input type="checkbox" id="cb01" checked="checked" runat="server" />
        <label for="cb01">All</label>
    </div>
    <div class="dvFilter">
        <input type="checkbox" id="cb02" checked="checked" runat="server" />
        <label for="cb02">None</label>
    </div>
</asp:Panel>

Code behind

foreach (Control item in pnlFilter.Controls)
{
    HtmlInputCheckBox _cbx = item as HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
1

I think the answer is a combination of all the suggested changes. Make the checkboxes runat server as to have 'm available server-side:

<asp:Panel ID="pnlFilter" runat="server">
    <div class="dvFilter">
        <input type="checkbox" id="cb01" checked="checked" runat="server" />
        <label for="cb01">All</label>
    </div>
    <div class="dvFilter">
        <input type="checkbox" id="cb02" checked="checked" runat="server" />
        <label for="cb02">None</label>
    </div>
</asp:Panel>

And as suggested interate the (sub)controls of the panel:

foreach (Control item in this.pnlFilter.Controls)
{
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = item as System.Web.UI.HtmlControls.HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }

}

Or if you changed the checkboxes to asp:Checkbox then the type would be System.Web.UI.WebControls.CheckBox

Frank Witte
  • 466
  • 5
  • 17
1

I think it would be better to access front-end stuff by front-end process.

For example, collect checked checkbox ids you need by JQuery and save them inside HiddenField then get it back-end by request.

Instance

<asp:Panel ID="pnlFilter" runat="server">
    <div class="dvFilter">
        <input type="checkbox" id="cb01" checked="checked" />
        <label for="cb01">All</label>
    </div>
    <div class="dvFilter">
        <input type="checkbox" id="cb02" checked="checked" />
        <label for="cb02">None</label>
    </div>
    <input type="hidden" id="hdfIds" name="hdfIds" />
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" />

JQuery part(link JQuery & json2 lib first)

function recoverCbxStatus() {
    if (hdfIds.value) {
        var ids = JSON.parse(hdfIds.value);
        ids.forEach(function (id) {
            $('#' + id).prop('checked', true);
        });
    }
}

function refreshIds() {
    var ids = [];
    $('input:checkbox[id^="cb"]:checked').each(function () {
        ids.push($(this).attr("id"));
    });
    hdfIds.value = JSON.stringify(ids);
}

$(document).ready(function () {
    recoverCbxStatus();
    refreshIds();
});

$('input:checkbox[id^="cb"]').change(function () {
    refreshIds();
});

Back-end(Nuget Json.NET)

protected void Page_Load(object sender, EventArgs e)
{
    string idsJson = Request["hdfIds"];
    List<string> ids;
    if (idsJson != null)
    {
        ids = JsonConvert.DeserializeObject<List<string>>(idsJson);
        Response.Write("count: " + ids.Count);
    }
}
Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37
0

You can use GridView. And code side do this-

for (int i = 0; i < **yourGridViewId** .Rows.Count; i++)
    {

CheckBox cb = ((CheckBox)**yourGridViewId** .Rows[i].FindControl(" **yourCheckboxId**"));
....

}
Si8
  • 9,141
  • 22
  • 109
  • 221