C# ASP.NET C使用ITemplate在代码端动态生成复选框触发

C# ASP.NET C使用ITemplate在代码端动态生成复选框触发,c#,asp.net,gridview,datatable,datagrid,C#,Asp.net,Gridview,Datatable,Datagrid,我想做一个里克特秤 问题1: 将创建在文本框中输入的列数。列的名称从1开始到给定的数字。单击使用itemplate创建的复选框不会触发 问题2: 假设我们没有费心去触发复选框。我怎样才能得到复选框?即使我使用for导航网格,也无法接收它 注意:保存选择后,必须执行更新和删除过程。在保存期间,它应该浏览网格并获得标记的复选框 SQL C:AddTemplateToGridView.cs WebForm1.aspx.cs WebForm1.aspx设计源代码 public class AddTem

我想做一个里克特秤

问题1: 将创建在文本框中输入的列数。列的名称从1开始到给定的数字。单击使用itemplate创建的复选框不会触发

问题2: 假设我们没有费心去触发复选框。我怎样才能得到复选框?即使我使用for导航网格,也无法接收它

注意:保存选择后,必须执行更新和删除过程。在保存期间,它应该浏览网格并获得标记的复选框

SQL C:AddTemplateToGridView.cs WebForm1.aspx.cs WebForm1.aspx设计源代码
public class AddTemplateToGridView : ITemplate
{
    ListItemType _type;
    string _colName;
    DataTable dt2;
    int sayi = 0;
    public AddTemplateToGridView(ListItemType type, string colname, DataTable dt)
    {
        _type = type;

        _colName = colname;
        dt2 = dt;

    }
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {

        switch (_type)
        {
            case ListItemType.Item:


                sayi++;
                CheckBox checkbox = new CheckBox();
                //checkbox.Attributes.Add("onclick", "javascript:SingleCheckboxCheck(this)");
                checkbox.EnableViewState = true;
                
                checkbox.Enabled = true;
                //checkbox.AutoPostBack = true;
                checkbox.CheckedChanged += new EventHandler (this.Checkbox_CheckedChanged);

                container.Controls.Add(checkbox);

                break;
        }

    }

    protected void Checkbox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (sender as CheckBox);
        if (chk.Checked)
        {
            
            
        }
    }
    
}
public partial class WebForm1 : System.Web.UI.Page
{

    SqlConnection cnn;
    SqlCommand cmd;

    string connetionString = "Server= localhost\\sqlexpress; Database= FormApp; Integrated Security=True;";
    int sayi;


    DataColumn column;
    DataRow row;
    DataTable dt;

    protected void btnCreateLickertScaleGrid_Click(object sender, EventArgs e)
    {


        dt = new DataTable();
        grdData.Columns.Clear();
        dt.Columns.Clear();

        cnn = new SqlConnection(connetionString);
        cmd = new SqlCommand("select job_Name from tbl_Job", cnn);
        cnn.Open();
        SqlDataReader dataReader = cmd.ExecuteReader();

        column = new DataColumn();
        column.ColumnName = "Job Name";
        dt.Columns.Add(column);
        while (dataReader.Read())
        {
            dt.Rows.Add(dataReader[0]);

        }
        cnn.Close();
        dataReader.Close();
        int columnCount = int.Parse(txtGroupCount.Text.ToString());

        for (int i = 0; i < columnCount; i++)
        {
          
            TemplateField lname = new TemplateField();

            lname.HeaderText = (i + 1).ToString();
            lname.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, "", dt);
            grdData.Columns.Add(lname);
        }
        grdData.DataSource = dt;
        grdData.DataBind();
    }
}
<body>
<form runat="server">
    <div id="container">
        <div id="navigation">
            <asp:Label ID="lblGroupCount" runat="server" Text="Group Count:"></asp:Label>
            <asp:TextBox ID="txtGroupCount" runat="server"></asp:TextBox>
            <asp:Button ID="btnCreateLickertScaleGrid" runat="server" Text="Create Lickert Scale" CssClass="btn btn-info" OnClick="btnCreateLickertScaleGrid_Click"/>
            <br />
        </div>
        <div id="grd">

            <asp:GridView ID="grdData" runat="server" CssClass="table table-condensed table-hover" Width="50%"
                AutoGenerateColumns="True" CellPadding="4" EnableTheming="True" OnRowDataBound="grdData_RowDataBound">
                <AlternatingRowStyle BackColor="White"
                    ForeColor="#284775"></AlternatingRowStyle>
               
                
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True"
                    ForeColor="White"></FooterStyle>
                <HeaderStyle BackColor="#3333ff" Font-Bold="True" HorizontalAlign="Left"
                    ForeColor="White"></HeaderStyle>
                <PagerStyle BackColor="#284775" ForeColor="White"
                    HorizontalAlign="Center"></PagerStyle>
                <RowStyle BackColor="#f8efd4" ForeColor="#333333"></RowStyle>
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
                    ForeColor="#333333"></SelectedRowStyle>
                <SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>
                <SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>
                <SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>
                <SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
            </asp:GridView>
            <asp:Panel ID="pnlLickert" runat="server">
             <asp:Button ID="btnSaveLickertScale" runat="server" CssClass="btn btn-success" Text="Save Lickert Scale" OnClick="btnSaveLickertScale_Click"/>
            <asp:Button ID="btnUpdateLickertScale" runat="server" CssClass="btn btn-primary" Text="Update Lickert Scale" OnClick="btnFormOlustur_Click"/>
            <asp:Button ID="btnDeleteLickertScale" runat="server" CssClass="btn  btn-danger" Text="Delete Lickert Scale" OnClick="btnFormOlustur_Click"/>
            </asp:Panel>
        </div>
    </div>
</form>