如何从asp.net?

时间:2017-07-27 12:04:09

标签: c# asp.net-web-api gridview

我有一组没有名称的输入复选框,我想从gridview获取那些我只检查过的值,我该如何实现?我真的很难完成它(BtnAdd_Click)。也许你可以帮忙?以下是.aspx和.aspx.cs代码:

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

这是按钮点击事件:

 protected void BtnAdd_Click(object sender, EventArgs e)
            {

            }

3 个答案:

答案 0 :(得分:0)

您可以在gridview上应用迭代来获取所选的复选框值:

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

答案 1 :(得分:0)

我做到了,但是当我被引导到下一页时没有显示任何值。

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

答案 2 :(得分:0)

在按钮事件中尝试此操作:

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

        }
    }
}