0

I am using asp.net with c# and I am trying to create checkbox dynamically from code and add it to my page here is my c# code in file aspx.cs

public String getSubjects()
{
    SqlConnection conn = new DB_Connection().getConnection();
    SqlCommand cmd = new SqlCommand("select * from subjects", conn);

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    String data = "";
    while (reader.Read())
    {
        CheckBox checkbox = new CheckBox();
        checkbox.ID =Convert.ToString(reader.GetInt32(0));           

        data += "<tr><td>" +checkbox+ "</td><td>" + reader.GetString(1) + "</td></tr>";

    }
    conn.Close();
    return data;
}

and here is my design code in aspx

<table class="table table-responsive table-bordered table-striped">
    <tr>
        <td>check</td>
        <td>subject name</td>

    </tr>
    <%=getSubjects()%>
</table>

when i run the code it works good but the check books appear like this

System.Web.UI.WebControls.CheckBox

3 Answers 3

4

Here is a quick example of how you should be working with Dynamic Controls and how to add them to a Page. Or in your case create a table and then add it to the page. You cannot create a Control and add it to the page as a string.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        //not in an ispostback check
    }
    //but outside when working with dynamic controls

    //create a new table
    Table table = new Table();

    //create a connection and a command
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    using (SqlCommand cmd = new SqlCommand("select * from subjects", conn))
    {
        //open the connection
        conn.Open();

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                //create a new row, cell and checkbox
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                CheckBox cb = new CheckBox();

                //set some checkbox properties
                cb.Text = reader["ColumnName"].ToString();

                //add the checkbox to the cell
                cell.Controls.Add(cb);

                //the cell to the row
                row.Controls.Add(cell);

                //and the row to the table
                table.Controls.Add(row);
            }
        }
    }

    //finally add the table to the page
    PlaceHolder1.Controls.Add(table);
}
Sign up to request clarification or add additional context in comments.

1 Comment

many thank your solution give me more knowledge for another way to solve this issue
1

NOTE: I realize this will not solve your problem with the way your existing code is but this is to show you how you would do it. It will require you to rewrite some stuff in your function.

The issue is you are trying to build a string with your HTML in it but adding an object that is a checkbox.

You need to do a Controls.Add call.

If you want it in a specific spot you need to use some container and give it a name then add the checkbox to that object.

<div id="CheckBoxAdd"></div>

Then in code behind add this to your code:

CheckBoxAdd.Controls.Add(checkbox);

What you can do is use HTML to build out the checkbox using this HTML for the checkbox like you are for the HTML for your table:

<input type="checkbox" name="vehicle" value="Bike">

So in your loop you could do this:

data += "<tr><td><input type='checkbox' name='ComboBox" + reader.GetInt32(0) + "' value=''></td><td>" + reader.GetString(1) + "</td></tr>";

3 Comments

thank you for your reply i am thinking how to change the way of writing the bode the input html tag is not useful because the number of rows and cells depends on database data
For the checkbox you could set the name/id column like you are in your loop for the object, but use it in the string instead so the name/ID in the checkbox would be built like this: "<input type = 'checkbox' name='ComboBox" + reader.GetInt32(0) + "' value=''>" (i used singlee quotes to make it easier to read
Moved my code from comment to my answer to make it easier to read
1

Just for adding to the question, you can actually return a control through a string to the page like below:

public string createCheckControlWithString()
{
    CheckBox ck = new CheckBox();
    ck.ID = "Checkbox";
    ck.Checked = false;
    ck.Text = "Please Tick This";
    StringWriter sw = new StringWriter();
    HtmlTextWriter writer = new HtmlTextWriter(sw);
    ck.RenderControl(writer);
    return sw.ToString();
}

and use in the page as you have used it

<%=createCheckControlWithString()%>

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.