Reputation: 593
I'm brand new to ASP.NET with intermediate C# level, and previously I wrote a PHP project. I'm now stuck trying to get a similar effect in ASP.NET.
What I'm using:
The project is C# ASP.NET Empty web application
. I'm using Visual Studio 2010 with SP1.
MSSQL 2008 R2
What I want to do is add HTML code using a foreach into the ASP file, specific content area.
This is what I would do in php:
foreach ($library as $book)
{
print "<a href=\"bookpage.php?id=".$book[book_id]"";
print "<h3>".$book[book_author]."</h3>";
print " <p>".$book[book_blurb]."</p>";
}
This is what I've tried in ASP:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:RadioButtonList ID="RadioButtonPizzaList" runat="server">
<asp:ListItem Text="Margerita" />
<asp:ListItem Text="Hawaain" />
<asp:ListItem Text="Meat Supreme" />
</asp:RadioButtonList>
</asp:Content>
But instead of hardcoding, I want to add a listitem
for each pizza
thats been retrieved from the database, the names of pizza
are stored in an array. How would I use a loop and add an HTML line like what I did in above PHP example?
Upvotes: 0
Views: 2804
Reputation: 593
In the end, the answer to my problem was using c# to create the table and add rows, and in the cells add the content. One of the questions around here while I was looking around was kind of answered, but not fully. So if you see this you can adapt it.
I've changed from using checkboxes to simply adding text to the cell area, but to answer my original question, one can do what I did with the textboxes and choose which cells to add the checkboxes, or simply do without the table and loop checkboxes into your wanted area.
At the end I add the table to a div that is ready made in the asp code
public partial class _Default : System.Web.UI.Page
{
Database doDatabase = new Database();//custom class for querying a database
ArrayList textboxNames = new ArrayList();//just to make life easier
ArrayList pizzaNames = new ArrayList();//just to make life easier
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
this.FillPizzaList();
}
private void FillPizzaList()
{
int count = 1;
string query = "select FoodID, FoodName, 'R' + Convert(char(10), FoodPrice)[Food Price], rtrim(FoodDesc)[FoodDesc] from tblFood";
doDatabase.Do_SQLQuery(query);
Table tablePizza = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
for (int c = 0; c < 4; c++)
{
tc = new TableCell();
if (c == 0)
{
tc.Width = new Unit("15%");
tc.Text = "<h3>Pizza Name</h3>";
}
else if (c == 1)
{
tc.Text = "<h3>Pizza Description</h3>";
}
else if (c == 2)
{
tc.HorizontalAlign = HorizontalAlign.Center;
tc.Width = new Unit("15%");
tc.Text = "<h3>Pizza Price</h3>";
}
else if (c == 3)
{
tc.Width = new Unit("12%");
tc.Text = "<h3>Pizza Quantity</h3>";
}
tr.Cells.Add(tc);
}
tablePizza.Rows.Add(tr);
foreach (DataRow dr in doDatabase.dataTbl.Rows)
{
tr = new TableRow();
for (int c = 0; c < 4; c++)
{
tc = new TableCell();
if (c == 0)
{
pizzaNames.Add(dr["FoodName"].ToString());
tc.Text = dr["FoodName"].ToString();
}
else if (c == 1)
{
tc.Text = dr["FoodDesc"].ToString();
}
else if (c == 2)
{
tc.HorizontalAlign = HorizontalAlign.Center;
tc.Text = dr["Food Price"].ToString();
}
else if (c == 3)
{
TextBox MyTextBox = new TextBox();
MyTextBox.ID = "Quantity" + count;
textboxNames.Add("Quantity" + count);
MyTextBox.Text = "0";
tc.Controls.Add(MyTextBox);
count++;
}
tr.Cells.Add(tc);
}
tablePizza.Rows.Add(tr);
}
pizzaMenu.Controls.Add(tablePizza);//add table to div
}
Upvotes: 0
Reputation: 1038
I assume that your pizza list has two columns that Id and PizzaName.
And you have a method that getting pizza list from db as named GetList in Pizza class.
Firstly you should add two attributes in aspx side to your radio button list control.
They are DataValueField and DataTextField.
These attributes required for data binding.
Aspx Side
<asp:RadioButtonList ID="RadioButtonPizzaList" DataValueField="Id" DataTextField="PizzaName" runat="server">
</asp:RadioButtonList>
Code behind Side
private void FillPizzaList()
{
DataTable dtList = Pizza.GetList();
this.RadioButtonPizzaList.DataSource = dtList;
this.RadioButtonPizzaList.DataBind();
}
If you want to get selected item value you can get with this code
this.RadioButtonPizzaList.SelectedValue
Note: If you fill radiobutton list in page load event, do not forget check is postback.
if ( !IsPostBack )
this.FillPizzaList();
Upvotes: 1