2013-03-13 69 views
3

我想创建一个ASP.NET页面上的动态Gridview,我有多行,并添加了CheckBox列。ASP.NET隐藏GridView行与复选框

<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数据库构建的,所以它必须是动态的。非常感谢!

回答

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

    } 
} 
+0

资本'V'in'.Visible',傻= = – jadarnel27 2013-03-13 13:02:52

+0

@ jadarnel27固定,谢谢证明:D – tymeJV 2013-03-13 13:07:04

+0

哈哈,没问题。此外,您从未在您的示例中声明“复选框”,并且“.Checked”属性也应该大写。您应该在发布之前在IDE中检查这些内容,以确保安全。 – jadarnel27 2013-03-13 13:10:27

0

我会使用JavaScript这样的事情。