ASP.NET使用Checkbox隐藏Gridview行

时间:2013-03-13 12:54:27

标签: asp.net gridview checkbox

我正在尝试在ASP.NET页面上创建动态Gridview,并且我有多行,并添加了一个CheckBox列。

<body>
    <h1>Alerts</h1>
    <form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KiwiLogConnectionString %>" SelectCommand="SELECT * FROM [Syslogd]
GROUP BY MsgHostname, MsgDate, MsgTime, MsgPriority, MsgText"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="#333333" AllowPaging="True" PageSize="15">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField >
                <ItemTemplate >
                <asp:CheckBox ID ="Checkbox" runat="server"/>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="MsgDate" HeaderText="Datum" SortExpression="MsgDate" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgTime" HeaderText="Tijd" SortExpression="MsgTime" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgPriority" HeaderText="Priority" SortExpression="MsgPriority" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgHostname" HeaderText="Hostname" SortExpression="MsgHostname" ItemStyle-Wrap="false" >
                    <ItemStyle Wrap="False"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="MsgText" HeaderText="Message" SortExpression="MsgText" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:Button ID="btn_update" runat="server" OnClick="btn_update_Click" Text="Update" />
    </form>
  </body>

如果选中CheckBox并单击“更新”按钮,我希望隐藏这些行。我怎么能设法做到这一点?

protected void btn_update_Click(object sender, EventArgs e)
{

}

gridview是使用SQL数据库构建的,因此它必须是动态的。非常感谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

protected void btn_update_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GridView1.Rows) 
    {
            if (((CheckBox)gvr.findcontrol("Checkbox")).Checked == true)
            {
                //Do stuff with checked row
                gvr.Visible = false;
            }

    }
}

答案 1 :(得分:0)

我会使用javascript这样的东西。