2011-04-06 46 views
0

我在数据列表有DDL,当我试图从DDL这个误差值绑定数据列表apeared(对象refrence没有设置...)这里DDLProduct.SelectedIndex错误时绑定DataList控件与值从DDL

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(); 

      } 
     } 
    } 
+1

完整例外,行进线将是有益的,还有ASPX定义。 – asawyer 2011-04-06 14:36:35

回答

0

我认为你的问题在这里:

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

请告诉我此行的目的是什么?

你已经有一个参考这个控制,“DDLProduct”

编辑 - 没有实际的异常ECT我只是猜测。

0

什么行会给你错误?

只是一个猜测,但尝试

DDLProduct = sender as DropDownList;