如何动态创建自定义控件?

时间:2021-10-04 01:21:09

Let me get this straight. I manage to create a custom Gridview with a single checkbox but my problem here is that I want to include multiple dynamic Checkbox.

让我说清楚。我设法用一个复选框创建一个自定义Gridview,但我的问题是我想要包含多个动态Checkbox。

Here is my code:

这是我的代码:

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

In this gridview I only have a checkboxes at the first column. I want to put the dynamically generated checkboxes at the last column.

在这个gridview中,我在第一列只有一个复选框。我想将动态生成的复选框放在最后一列。

P.S.: How do I retrieve the id with those dynamically create checkboxes?

P.S。:如何使用那些动态创建复选框来检索id?

1 个解决方案

#1


1  

You can do like this

你可以这样做

  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
 }

#1


1  

You can do like this

你可以这样做

  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
 }