I have a stored procedure that returns a number of rows based on the number of records stored in the database, now I want to have a way of creating a <div>
tag with controls containing values from that row, if there are 10 rows returned from the database, then 10 <div>
tags must be created, I have the code below to fetch the results from the database but I don't know how to continue from here.
String sql = "exec dbo.spLoadCandidates @NationalID, @PostID";
SqlDataReader reader;
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@NationalID",TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@PostID,", DropDownList1.SelectedValue);
conn.Open();
reader = cmd.ExecuteReader();
while (reader.HasRows)
{
//Dynamic Data here
}
conn.Close();
}
update: Am using web forms to do this
You got several alternatives.
Alternative 1. Add a
PlaceHolder
to your aspx, and fill it with the contents you want.Then you can add contents to this
PlaceHolder
from code behind.Alternative 2. Use a
Repeater
to your aspx.And your code behind:
And your code:
Since you are using
ASP.NET WebForms
I suggest you to use Repeater.but if you prefer to do it with code, here's a workaround:
You can use a Repeater control for rendering any custom markup for a collection of items. You can try this:
Markup:
Code behind: bind your Sql result to the Repeater control.