How to count the checkboxes in a asp.net form

2019-06-06 08:13发布

问题:

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.

回答1:

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))



回答2:

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.



回答3:

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 />");
        }
    }
}


回答4:

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 />");
        }
    }
}


回答5:

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



回答6:

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);
    }
}


回答7:

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**"));
....

}