将datalist与ddl中的值绑定时出错

时间:2011-04-06 14:31:59

标签: asp.net drop-down-menu

我在datalist中有ddl,当我尝试使用ddl中的值绑定datalist时,此错误apeared(对象引用未设置...)此处DDLProduct.SelectedIndex

public DropDownList DDLProduct;

    protected void Page_Load(object sender, EventArgs e)
    {
     }

 protected void DLProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
            DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
        }

    }
  protected void DDlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLProduct.SelectedIndex > 0)
        {
            using
            (SqlConnection conn = Connection.GetConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_GetProductsByProductID";
                SqlParameter ParentID_Param = cmd.Parameters.Add("@ProductID", SqlDbType.Int);
                ParentID_Param.Value = DDLProduct.SelectedValue;
                ;
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
                DLProduct.DataSource = dt;
                DLProduct.DataBind();

            }
        }
    }

2 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;

这条线的目的是什么?

您已经引用了此控件“DDLProduct”

编辑 - 没有实际的例外,我只是猜测。

答案 1 :(得分:0)

什么行给你错误?

只是一个猜测,但尝试

DDLProduct = sender as DropDownList;