1

I am trying to reference a non-asp check box in C# code behind. The reason the checkbox is not an asp element, is it gets auto-generated on the fly, rather than being a part of the website. So far I have the following relevant 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> 

And the C# code behind is:

    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 is always null :(.

3
  • Please wrap it in like an <asp:Panel ID="Pnl" runat="server"> first and use Pnl.FindControl("chk1"); Commented Aug 26, 2018 at 6:41
  • Is this a stand-alone .aspx page, or does it have a master page? The latter has a tendency to change the generated ID. Can you edit an example of the rendered input? Commented Aug 26, 2018 at 6:41
  • I have tried Robs suggestion, and it worked but only with checkboxes manually added in the aspx. It does have a master page, I have updated how the checkboxes are added. Thanks. Commented Aug 30, 2018 at 22:32

1 Answer 1

1

You'll need to change two things.

In order to find a checkbox via FindControl it must be part of the pages control collection, which means you have to add a CheckBoxcontrol.

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

The dynamically added CheckBox control is part of the control collection of the Table, so you'll have to search for it there instead of on the page.

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

Below you find a full update of your code.

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 :(");                           
    }
}                     
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.