0

I am trying to format items in shopping cart that I display in listbox in ASP.NET using MS Visual Studio 2010. The program displays them elements of each item as a concatenated string, which works but is less elegant than I would like.

My display function within the cartItem Class is:

Public Function Display() As String Return Product.ProductName & "; " & Product.ProductID & ", (" + Quantity.ToString() & " at " & FormatCurrency(Product.UnitPrice) & " each) " End Function

I was told to "embed HTML to format your string. Add a table, trs and tds and CSS." I looked up "Embed HTML" in W3Schools and got nothing that seemed relevant. I could not find anything like this in my class textbook. When I try to insert statements like tags like and Visual Studio transforms them into and . I tried putting statements into response write as follows:

    Response.Write("<html>")
    Response.Write("<table>")
    Response.Write("<tr>")
    Response.Write("<td>")
    Response.Write("<Product.ProductID>")
    Response.Write("</td>")
    Response.Write("<td>")
    Response.Write("<Product.ProductName>")
    Response.Write("</td>")
    Response.Write("</tr>")
    Response.Write("</table>")

However, I got an error on each line saying "Response is not declared. It may be inaccessible due to its protection level". Even if it worked, I am not certain that Response.Write would be appropriate to format something being printed inside a list box.

I could try to use another control (Detail View, Form View) to display the cart items, but I have only learned how to bind those to databases, and these items are in my cart (an object of the cartitemlist class, which is an array of cart items), not in a database.

Any help would be greatly appreciated. I need to learn this so I am not just looking for a solution to copy; I want to understand how to do this right. Thank you in advance. W

1 Answers1

1

You could create an usercontrol and implement RenderContents();

public class ProductGrid :  System.Web.UI.WebControls.WebControl
{
    public List<Product> DataSource {get; set;}

    protected override void RenderContents(HtmlTextWriter output)
    {
        //here you can iterate through datasource and write the output
        output.Write("<html><table>");

        foreach(row in this.DataSource)
        {
            output.Write(String.Format("<td>{0}</td><td>{1}</td></tr>",row.ProductID, row.ProductName));
        }

        output.Write("</table></html>");
    }
}

Now place this usercontrol in aspx page, for example could be in a div or td. Remember if you are creating this user control in a separate assembly refer that in the aspxpage using "Register Assembly"

<div>
     <ProductGrid Width="100%" ID="ProductGrid1" runat="server" />
</div>

And set the datasource in Page_Load

ProductGrid1.DataSource = ??? ; //from where you get List<Product>
vinodpthmn
  • 1,062
  • 14
  • 28