2011-04-06 40 views
0

我有datalist,我有ddl,用户可以从ddl中选择值来绑定datalist。当我尝试在datalist中找到ddl当用户选择值时,此错误对应从DDL中选择的值时出错

索引超出范围。必须为 非负值且小于 的大小。参数名:索引

这里

DDLProduct2 = (DropDownList)DLProduct.Items[DLProduct.EditItemIndex].FindControl("DDlProduct"); 

所以请任何一个帮助我。

private DropDownList DDLProduct2; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ProductByProductID(); 
      CategoryParentZero(); 


     } 
    } 
protected void DDlProduct_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DDLProduct2 = (DropDownList)DLProduct.Items[DLProduct.EditItemIndex].FindControl("DDlProduct"); 
     if (DDLProduct2 != null) 
     { 
      if (DDLProduct2.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 = DDLProduct2.SelectedValue; 
        ; 
        DataTable dt = new DataTable(); 
        SqlDataAdapter da = new SqlDataAdapter(); 
        da.SelectCommand = cmd; 
        da.Fill(dt); 
        DLProduct.DataSource = dt; 
        DLProduct.DataBind(); 

       } 
      } 
     } 

    } 

回答

0

DLProduct.EditItemIndex应等于-1,因为没有什么编辑

只是前行添加if DLProduct.EditItemIndex >= 0引发错误