2013-01-22 74 views
0

讓我直說吧。我設法用一個checkbox創建一個自定義Gridview,但我的問題在於我想包含多個動態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放在最後一列。

P.S .:我如何檢索ID與那些動態創建checkboxes

+0

請參考以下鏈接: http://stackoverflow.com/questions/9527908/add-checkbox-with-checkedcahnged-event-to-a-dynamic -gridview – Kiran1016

回答

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 
} 
+0

我有一個現有的用戶控件該gridview,我要補充那裏?如果我在那裏添加,我可以在DataBind()函數中添加複選框。假設我的數據來自DataTable。 –

+0

你可以在databound事件或rowcreated事件中執行它 – Kiran1016