0

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?

4
  • Is asp.net web forms required? If not, use asp.net mvc which is much closer to the way you do things in php Commented Nov 3, 2013 at 20:21
  • Rather than looping through you'll find it much easier to set the datasource to array - better yet use a dictionary so the list holds both the description and key if you will use it later. Commented Nov 3, 2013 at 20:41
  • @Kevin: That is alot simpler, thanks Commented Nov 3, 2013 at 20:44
  • The problem is with setting a datasource to datatable, it might do the list automatically, but it doesn't then do the lables/text such as price and/or description Commented Nov 4, 2013 at 1:49

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

}

5 Comments

Why do not you use repeater control?
And why you do not define arraylist variables in the method? Why you do not call your fill method in IsPostBack control?
@Uğur Aldanmaz How would I go about using a repeater control in above code? I'm keen to learn the tricks of asp, and once I've finished exams will build a copy of my site using mvc, since it seems thats the modern framework to use
@Uğur Aldanmaz, also if I call my this.FillPizzaList(); inside if (IsPostBack) { this.FillPizzaList(); } then it doesn't load the table or so, and if I call it in if (!IsPostBack) { this.FillPizzaList(); } Then it loads but when I press order button it dissappears
Sorry, couldn't edit above comment. By having it outside if statement, if loads on page load, and stays after I press button. I don't understand why, and would prefer to solve this to a correct way of doing it. But it works, and for now I can live with myself.

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.