简体   繁体   中英

How to get values from multiple checked checkboxes in asp.net?

I have a set of input checkboxes without name and I would like to get the values from gridview of those that I have only checked, how can I achieve that? I am really struggling to get it done (BtnAdd_Click). Maybe you could help? Below are the .aspx and .aspx.cs codes:

    <asp:GridView ID="GvModules"
        AutoGenerateColumns="False"
        AllowSorting ="True"
        OnSorting="GvModules_Sorting"
        runat="server"
        CssClass="table table-bordered table-striped table-condensed table-hover table-responsive" OnSelectedIndexChanged="GvModules_SelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="moduleid" HeaderText="Module ID" />
            <asp:BoundField DataField="module_name" HeaderText="Module Name" />
            <asp:BoundField DataField="descriptions" HeaderText="Module Descriptions" />
            <asp:BoundField DataField="msa_DateTime" HeaderText="Start Date/Time" />
            <asp:TemplateField HeaderText="Check To Indicate">
                <ItemTemplate>
                    <asp:CheckBox ID="ChkAdd"
                        runat="server"
                        Text=""
                        CommandArgument='<%#Eval("moduleid") %>'
                        OnCommand="ChkAdd_Command"
                        OnClientClick='<%# "javascript:return confirm(\"Add [" + Eval("module_name") + "]\");"  %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
<div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
        <asp:Button ID="BtnAdd" CssClass="btn btn-default" Text="Indicate" OnClick="BtnAdd_Click" runat="server" />
    </div>

</div>
<div class="row">
    <hr />
    <asp:Literal ID="LtlSchool" runat="server" />
</div>

This is Button click event:

 protected void BtnAdd_Click(object sender, EventArgs e)
            {

            }

you can apply iteration on gridview to get the selected checkbox values:

protected void BtnAdd_Click(object sender, EventArgs e)
            {
foreach (GridViewRow r in GvModules.Rows)
            {
                //Find the checkbox in the current row 
                CheckBox chk = (CheckBox)r.FindControl("ChkAdd");


                if (chk!=null && chk.Checked)
                {
                    //code when checkbox checked
                }
            }
      }

I did it, but no values were displayed when I got directed to the next page.

            if (chk!=null && chk.Checked)
            {
                Response.Redirect("AssociateIndication.aspx");
            }
        }
    }

Try this in button event:

protected void BtnAdd_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GvModules.Rows)
    {
        CheckBox chk = (CheckBox)gvr.FindControl("ChkAdd");

        if (chk.Checked == true)
        {
           // do something here if checkbox is checked

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM