前台:商品分类的下拉框
<asp:Literal ID="Literal1" runat="server" Text="商品分类" />:
<asp:DropDownList ID="drpProductCategory" runat="server">
</asp:DropDownList>
后台:Page_Load()中调用BindCategories()
private void BindCategories()
{
DataSet list = new Maticsoft.BLL.Shop.Products.CategoryInfo().GetList(" Depth = 1 ");//从数据库查出来的DataSet
if (!DataSetTools.DataSetIsNull(list))
{
this.drpProductCategory.DataSource = list; //赋值给控件的DataSource
this.drpProductCategory.DataTextField = "Name"; //其中数据库的Name属性的值复制给DataTextField
this.drpProductCategory.DataValueField = "CategoryId"; //数据库的CategoryId属性的值复制给DataValueField
this.drpProductCategory.DataBind(); //绑定数据
}
this.drpProductCategory.Items.Insert(0, new ListItem("请选择", string.Empty));
}
.Net项目实战:DropDownList 用法
最新推荐文章于 2024-04-10 21:15:25 发布
本文介绍如何使用ASP.NET在网页上实现商品分类的下拉框功能。具体步骤包括设置前台页面元素,如Literal和DropDownList,并在后台通过Page_Load事件调用BindCategories方法填充下拉框选项。此方法从数据库获取商品分类信息并将其绑定到DropDownList控件。
721

被折叠的 条评论
为什么被折叠?



