Reputation: 483
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
Upvotes: 0
Views: 6930
Reputation: 1509
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()%>
Upvotes: 1
Reputation: 3641
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>";
Upvotes: 1
Reputation: 35564
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);
}
Upvotes: 4