【问题标题】:ASP.NET Hide Gridview row with CheckboxASP.NET 使用复选框隐藏 Gridview 行
【发布时间】:2013-03-01 10:10:56
【问题描述】:

我正在尝试在 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>

如果选中该复选框并单击“更新”按钮,我希望隐藏这些行。我怎样才能做到这一点?

protected void btn_update_Click(object sender, EventArgs e)
{

}

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

【问题讨论】:

    标签: asp.net gridview checkbox


    【解决方案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;
                }
    
        }
    }
    

    【讨论】:

    • 哈哈,没问题。此外,您从未在示例中声明“复选框”,并且.Checked 属性也应该大写。为了安全起见,您可能应该在发布之前在 IDE 中检查这些内容。
    • @TymeJV,这不起作用。他不知道 .checked 属性。我正在 Visual Studio 2012 中构建网络表单,您有其他解决方案吗?
    • @BassieBas1987 我做了一些编辑。让我知道这是否有帮助:)
    【解决方案2】:

    我会使用 javascript 来做这样的事情。

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2016-05-03
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2014-07-06
      相关资源
      最近更新 更多