0

让我说清楚。我设法Gridview用单个创建自定义,checkbox但我的问题是我想包含多个动态Checkbox.

这是我的代码:

<%@ Control Language="C#" AutoEventWireup="true" EnableTheming="true" CodeFile="GridviewControl.ascx.cs" Inherits="UserControl_GridviewControl" %>

<link href="../Template/CSS/Style.css"type="text/css" rel="Stylesheet" />

<div>
  <span id="Span5" style="font-family: Arial; font-size:12px;" ><asp:Label ID="lblCount" runat="server" Text="0" Font-Bold="true"></asp:Label>
                     Record/s</span>
  <span class="maintenance-btns">
     <asp:Button ID="btnNew" runat="server" Text=" New " onclick="btnNew_Click"/>
     <asp:Button ID="btnDel" runat="server" Text=" Delete " onclick="btnDel_Click"/>
  </span>
</div>

<asp:Panel ID="pnlPc" runat="server" CssClass="div-grid" ScrollBars="Auto">  
<asp:GridView ID="gvListing" runat="server" AllowPaging="True" AutoGenerateSelectButton="true"
            OnRowDataBound="gvListing_RowDataBound" skinid="gvListing"
            onselectedindexchanged="gvListing_SelectedIndexChanged"
            onpageindexchanging="gvListing_PageIndexChanging" PageSize="50" >

<FooterStyle CssClass="grid-footer" />

<Columns>
<asp:TemplateField HeaderText="CheckAll">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" 
OnCheckedChanged="chkSelectAll_CheckedChanged" CssClass="select-all"/>
</HeaderTemplate>

<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" 
OnCheckedChanged="chkSelect_CheckedChanged" CssClass="listing-checkbox"/>
</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>
</asp:Panel>

在这gridview我只有checkboxes在第一列。我想把动态生成checkboxes的放在最后一列。

PS:如何通过动态创建的方式检索 id checkboxes

4

1 回答 1

1

你可以这样做

  protected void gvData_DataBound(object sender, EventArgs e) 
  {
    foreach (GridViewRow objRow in gvData.Rows)
    {
      TableCell tcCheckCell = new TableCell();
      var checkBox = new CheckBox();
      checkBox.CheckedChanged += checkBox_CheckedChanged;
      tcCheckCell.Controls.Add(checkBox);
      objRow.Cells.AddAt(0, tcCheckCell);
   }
  }
 void checkBox_CheckedChanged(object sender, EventArgs e)
 {
    //do something
 }
于 2013-01-22T10:23:22.443 回答