如何使用 Webforms 在 Asp.net 中按产品类型对产品进行分类
How to categorize product by it's types in Asp.net using Webforms
我想使用下拉列表按类型对产品进行排序。
当我在下拉列表中 select 按类型分类的产品不起作用。
ProductByType 的存储过程:
Public List<Product> GetProductsByType(int typeId)
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
//select * from table where condition is required type
List<Product> products = (from x in db.Products
where x.TypeId == typeId
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
用于显示产品的索引页代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
private void FillPage()
{
//Get a lsit of all products in DB
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();
//Make sure products exists in the database
if (products != null)
{
//Create a new Panel with an ImageButton and 2 labels for each Product
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/Pages/Product.aspx?id=" + product.Id;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "₹" + product.Price;
lblPrice.CssClass = "productPrice";
//Add child controls to Panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblPrice);
//Add dynamic Panels to static Parent panel
pnlProducts.Controls.Add(productPanel);
}
}
else
{
//No products found
pnlProducts.Controls.Add(new Literal { Text = "No Products Found!" });
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetProductsByType(Convert.ToInt32(DropDownList1.SelectedItem.Value));
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/Pages/Product.aspx?id=" + product.Id;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "₹" + product.Price;
lblPrice.CssClass = "productPrice";
//Add child controls to Panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblPrice);
//Add dynamic Panels to static Parent panel
pnlProducts.Controls.Add(productPanel);
}
}
}
即使我 select 产品类型,它也总是显示所有产品。
如图所示,我 select 编辑了“Engine Oil”,但它显示了所有产品。
我希望它仅显示在下拉列表中选择的 selected 产品类型的特定产品。
尝试在您的 Page_Load
事件中添加 PostBack
验证:
C#代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillPage();
}
}
最有可能的是,当在下拉列表中选择一个项目时,会生成一个回发,它将调用 PageLoad
并重新加载所有产品,为避免这种情况,通常设置回发验证
确保在 <asp:DropDownList />
控件中使用 AutoPostBack="True"
标志
我想使用下拉列表按类型对产品进行排序。
当我在下拉列表中 select 按类型分类的产品不起作用。
ProductByType 的存储过程:
Public List<Product> GetProductsByType(int typeId)
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
//select * from table where condition is required type
List<Product> products = (from x in db.Products
where x.TypeId == typeId
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
用于显示产品的索引页代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
private void FillPage()
{
//Get a lsit of all products in DB
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();
//Make sure products exists in the database
if (products != null)
{
//Create a new Panel with an ImageButton and 2 labels for each Product
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/Pages/Product.aspx?id=" + product.Id;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "₹" + product.Price;
lblPrice.CssClass = "productPrice";
//Add child controls to Panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblPrice);
//Add dynamic Panels to static Parent panel
pnlProducts.Controls.Add(productPanel);
}
}
else
{
//No products found
pnlProducts.Controls.Add(new Literal { Text = "No Products Found!" });
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetProductsByType(Convert.ToInt32(DropDownList1.SelectedItem.Value));
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/Pages/Product.aspx?id=" + product.Id;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "₹" + product.Price;
lblPrice.CssClass = "productPrice";
//Add child controls to Panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal { Text = "<br />" });
productPanel.Controls.Add(lblPrice);
//Add dynamic Panels to static Parent panel
pnlProducts.Controls.Add(productPanel);
}
}
}
即使我 select 产品类型,它也总是显示所有产品。 如图所示,我 select 编辑了“Engine Oil”,但它显示了所有产品。 我希望它仅显示在下拉列表中选择的 selected 产品类型的特定产品。
尝试在您的 Page_Load
事件中添加 PostBack
验证:
C#代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillPage();
}
}
最有可能的是,当在下拉列表中选择一个项目时,会生成一个回发,它将调用 PageLoad
并重新加载所有产品,为避免这种情况,通常设置回发验证
确保在 <asp:DropDownList />
控件中使用 AutoPostBack="True"
标志