下拉列表已选中

时间:2010-04-19 16:04:18

标签: c# asp.net

我有下拉列表,在特定条件下执行代码,我试图通过选中的值检查它,但是它得到错误

 protected void DDLProductFamily_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLProductFamily.Items.FindByText("Name").Selected == true)
        using (SqlConnection Con = Connection.GetConnection())
        {
            SqlCommand Com = new SqlCommand("GetListViewByProductCategory", Con);
            Com.CommandType = CommandType.StoredProcedure;
            Com.Parameters.Add(Parameter.NewInt("@ProductCategory_Id", DDLProductFamily.SelectedValue.ToString()));
            SqlDataAdapter DA = new SqlDataAdapter(Com);
            DA.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
        else if (DDLProductFamily.Items.FindByText("ProductFamilly").Selected == true)
        {
            using (SqlConnection Con = Connection.GetConnection())
        {
            SqlCommand Com = new SqlCommand("GetListViewByProductFamily", Con);
            Com.CommandType = CommandType.StoredProcedure;
            Com.Parameters.Add(Parameter.NewInt("@ProductFamily_Id", DDLProductFamily.SelectedValue.ToString()));
            SqlDataAdapter DA = new SqlDataAdapter(Com);
            DA.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
        }
    }

3 个答案:

答案 0 :(得分:1)

ProductFamily拼写错误(2 l而不是1)所以你得到一个空引用:

else if (DDLProductFamily.Items.FindByText("ProductFamilly").Selected == true)

答案 1 :(得分:1)

您是否实例化了dt对象?

答案 2 :(得分:0)

您可能需要检查ListeItemCollection方法FindByText

DDLProductFamily.Items.FindByText("Name").Selected

DDLProductFamily.Items.FindByText("ProductFamilly").Selected

如果使用“Name”或“ProductFamilly”条件在集合中找不到项目,则返回null。因此,如果在null对象上调用“Selected”,它将抛出一个null引用异常。

相关问题