0

下拉菜单忽略将默认行添加到列表

。CS

private const string DropDownListValue = "[All]";
private const string DropDownListText = "[All]";

 private void LoadProduct()
    {
        try
        {
            FieldGoalEntities objEntity = new FieldGoalEntities();

            IQueryable<Product> objProduct = from p in objEntity.Product
                                             select p;

            ddlProduct.Items.Clear();

            if (objProduct != null)
            {

                ddlProduct.DataSource = objProduct;
                ddlProduct.DataValueField = "ProductCode";
                ddlProduct.DataTextField = "ProductCode";
                ddlProduct.DataBind();
            }

            ddlProduct.Items.Insert(0, new ListItem(DropDownListText, DropDownListValue));
        }
        catch (Exception ex)
        {
            Common.LogEvent(Common.LoggingType.Error, "LoadProduct", ex.Message);
            Common.ShowMessageBox(this.Master, "Error", "Load Product Status: " +     ex.Message);
        }
    }

//ASPX

//下拉框

我需要为过滤添加所有内容,但由于某种原因,如果我在 .CS 文件中进行数据绑定,它会忽略 [ALL]

4

1 回答 1

0

我应该ddlProduct.DataBind();在您将所有添加到Items列表后致电

if (objProduct != null)
{

    ddlProduct.DataSource = objProduct;
    ddlProduct.DataValueField = "ProductCode";
    ddlProduct.DataTextField = "ProductCode";

}

ddlProduct.Items.Insert(0, new ListItem(DropDownListText, DropDownListValue));
ddlProduct.DataBind();
于 2013-01-03T06:48:42.573 回答