0

I am trying to create dinamically several asp check boxes in C# code behind with a specific class, but I am getting input elements without the class in a span element. Is this a normal behaviour of C#? My goal is to have all dynamically created checkboxes with same class in order to loop them afterwards and see if they are checked. Here is my C# code:

                Label data_month = new Label() { CssClass = "button" };
                data_month.Text = month;

                Label data_year = new Label() { CssClass = "button" };
                data_year.Text = year;

                Label data_plan = new Label() { CssClass = "button" };
                data_plan.Text = plan;

                CheckBox del_check = new CheckBox();
                del_check.ID = id;
                del_check.Attributes.Add("class", "delcheck_data");

                TableRow trow1_ex = new TableRow() { CssClass = "button" };
                TableCell tcell1_ex = new TableCell();
                TableCell tcell2_ex = new TableCell();
                TableCell tcell3_ex = new TableCell();
                TableCell tcell4_ex = new TableCell();

                table1_data.Rows.Add(trow1_ex);
                trow1_ex.Cells.Add(tcell1_ex);
                trow1_ex.Cells.Add(tcell2_ex);
                trow1_ex.Cells.Add(tcell3_ex);
                trow1_ex.Cells.Add(tcell4_ex);
                tcell1_ex.Controls.Add(data_month);
                tcell2_ex.Controls.Add(data_year);
                tcell3_ex.Controls.Add(data_plan);
                tcell4_ex.Controls.Add(del_check);

            }
            PlaceHolder1.Controls.Add(table1_data);

When I execute the code I get the following:

    <tr class="button">
    <td><span class="button">1</span></td>
    <td><span class="button">2010</span></td>
    <td><span class="button">Реални данни</span></td>
    <td><span class="delcheck_data">
        <input id="ContentPlaceHolder1_58" type="checkbox"
        name="ctl00$ContentPlaceHolder1$58"></span></td>
    </tr>

I am using master page and content pages if that matters.

1 Answer 1

1

You can set css class for a checkbox using InputAttributes

del_check.InputAttributes.Add("class", "delcheck_data");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick answer! It worked perfectly!

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.