Hej,
jeg har arbejdet en del med C#, men ikke så meget med asp delen.
Jeg har læst og lært om Eval, og bruger det nu, men
hvordan kan jeg lave eval, så det svarer til dette?
- foreach(Category cat in this.CategoryList)
- {
- Respone.Write("<div>" + cat.Name + "</div>");
-
- foreach(SubCategory subCat in this.SubCategoryList.Where(o => o.PID == cat.ID))
- {
- Respone.Write("<div>" + subCat.Name + "</div>");
- }
- }
Så langt er jeg nået:
- <asp:DataList ID="DataList_CategoryList" runat="server">
- <HeaderTemplate>
- </HeaderTemplate>
- <ItemTemplate>
- <div class="productCategory" style="margin-left: 25px;" runat="server">
- <div class="productCategoryName">
- <%# Databinder.Eval(Container.DataItem, "Name").ToString().Replace(Environment.NewLine, "<br />") %>
- </div>
- <div class="productSubCategory" style="margin-left: 25px;">
- <div class="productSubCategoryName">
- //************************************************//
- </div>
- </div>
- </div>
- </ItemTemplate>
- <FooterTemplate>
- </FooterTemplate>
- </asp:DataList>
- public partial class WebUserControl1 : System.Web.UI.UserControl
- {
- IList<Product> _productList;
- IList<Category> _categoryList;
- IList<SubCategory> _subCategoryList;
-
- public IList<Product> ProductList { get { return this._productList; } }
- public IList<Category> CategoryList { get { return this._categoryList; } }
- public IList<SubCategory> SubCategoryList { get { return this._subCategoryList; } }
-
- protected void Page_Load(object sender, EventArgs e)
- {
- this._categoryList = new List<Category>();
- this._subCategoryList = new List<SubCategory>();
- this._productList = new List<Product>();
-
- loadSubCategories();
- loadCategories();
- loadProducts();
-
- BindCategoryList();
- }
-
- private void BindCategoryList()
- {
- try
- {
- SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
- SqlCommand sqlCommand = new SqlCommand("select * from ProductCategories ", sqlConn);
-
- sqlCommand.Connection.Open();
-
- DataList_CategoryList.DataSource = sqlCommand.ExecuteReader();
- DataList_CategoryList.DataBind();
-
- sqlCommand.Connection.Close();
- sqlCommand.Connection.Dispose();
- }
- catch (Exception exc)
- {
- Console.WriteLine(exc.ToString());
- }
- }
-
- private void loadProducts()
- {
- this._productList = SqlClass.SelectProducts();
-
- this._productList.Add(new Product(0, "Shit", "This is a shitty product. 2kg.", 14.39, 7, 1, 0, false));
- this._productList.Add(new Product(1, "Crap", "This sure is crap! 2kg.", 299.95, 23, 1, 0, false));
- }
-
- private void loadSubCategories()
- {
- this._subCategoryList = SqlClass.SelectSubCategories();
- }
-
- private void loadCategories()
- {
- this._categoryList = SqlClass.SelectCategories();
- }
- }
På forhånd, tak for hjælpen
Indlæg senest redigeret d. 07.08.2011 05:55 af Bruger #14181