首页 > 解决方案 > 在后面的代码中引用非 asp 复选框

问题描述

我试图在后面的 C# 代码中引用一个非 asp 复选框。复选框不是 asp 元素的原因是它是动态自动生成的,而不是网站的一部分。到目前为止,我有以下相关的 aspx:

<asp:Table ID="myTable" runat="server" Width="100%"> 
    <asp:TableRow>
        <asp:TableCell>A</asp:TableCell>
        <asp:TableCell>B</asp:TableCell>
        <asp:TableCell>C</asp:TableCell>
        <asp:TableCell>D</asp:TableCell>
        <asp:TableCell>E</asp:TableCell>
    </asp:TableRow>
</asp:Table>
<asp:LinkButton runat="server" ID="TEST" CssClass="btn btn-default pull-right" OnClick="TEST_Click">
    TEST <i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton> 

后面的C#代码是:

    public void GenerateTable()
    {
        int i = 0;
        bool[] box = {true, false, true, false, true};
        List<TableRow> tRows = new List<TableRow>();
        TableRow newRow = new TableRow();
        tRows.Add(newRow);
        foreach (var check in box)
            {
                TableCell tempCell = new TableCell();
                if (check)
                {
                    tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" >";
                }
                else
                {
                    tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" checked>";
                }
                tRows[0].Cells.Add(tempCell);
                i++;
        }

        foreach (TableRow row in tRows)
        {
            myTable.Rows.Add(row);
        }
    }


    public void TEST_Click(object sender, EventArgs e)
    {

        HtmlInputCheckBox chkbox = (HtmlInputCheckBox)FindControl("chk1");
        if (chkbox != null)
        {
            if (!chkbox.Checked)
            {
                MessageBox.Show("Checked");
            }
            else
            {
                MessageBox.Show("NOT Checked");
            }
        }
        else
            MessageBox.Show("NOTHING :(");
    }

chkbox 始终为空 :(。

标签: c#asp.netwebforms

解决方案


你需要改变两件事。

为了通过FindControl它找到一个复选框,它必须是页面控件集合的一部分,这意味着您必须添加一个CheckBox控件。

CheckBox c = new CheckBox { ID = "chk"  + i };                
tempCell.Controls.Add(c);

动态添加CheckBox的控件是 的控件集合的一部分Table,因此您必须在那里而不是在页面上搜索它。

CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");

您可以在下面找到代码的完整更新。

protected void Page_Load(object sender, EventArgs e)
{
    GenerateTable();
}


public void GenerateTable()
{
    int i = 0;
    bool[] box = {true, false, true, false, true};
    List<TableRow> tRows = new List<TableRow>();
    TableRow newRow = new TableRow();
    tRows.Add(newRow);
    foreach (var check in box)
    {
        TableCell tempCell = new TableCell();
        CheckBox c = new CheckBox { ID = "chk"  + i };
        c.Checked = check;
        tempCell.Controls.Add(c);                    

        tRows[0].Cells.Add(tempCell);
        i++;
    }

    foreach (TableRow row in tRows)
    {
        myTable.Rows.Add(row);
    }
}


public void TEST_Click(object sender, EventArgs e)
{
    CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
    if (chkbox != null)
    {
        if (!chkbox.Checked)
        {
            MessageBox.Show("Checked");
        }
        else
        {
            MessageBox.Show("NOT Checked");
        }
    }
    else 
    {           
        MessageBox.Show("NOTHING :(");                           
    }
}                     

推荐阅读