2011-04-06 22 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;