如何使用Webforms在Asp.net中按产品类型对产品进行分类
我想使用dropdownlist按类型对产品进行排序 当我在下拉列表中选择“按类型划分的产品”时,它不起作用 ProductByType的存储过程:如何使用Webforms在Asp.net中按产品类型对产品进行分类,asp.net,webforms,dropdown,web-site-project,Asp.net,Webforms,Dropdown,Web Site Project,我想使用dropdownlist按类型对产品进行排序 当我在下拉列表中选择“按类型划分的产品”时,它不起作用 ProductByType的存储过程: Public List<Product> GetProductsByType(int typeId) { try { using (GarageDBEntities db = new GarageDBEntities()) {
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);
}
}
}
它总是显示所有的产品,甚至当我选择产品类型。
如图所示,我选择了发动机机油,但它显示了所有产品。
我希望它仅显示dropdownlist中所选产品类型的特定产品。尝试在页面加载事件中添加回发验证:
C代码:
最可能的情况是,在dropdownlist中选择一个项目时,会生成一个回发,该回发将调用PageLoad并重新加载所有产品,为了避免这种情况,通常会设置回发验证
确保在控件中使用AutoPostBack=True标志,表示catch块没有帮到您任何忙。至少,记录下你的错误!不要丢弃它。谢谢你@Julián的回复。成功了。过去三天来我一直很烦恼,你解决了。我很感激。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillPage();
}
}