【问题标题】:error when selected value from DDL从 DDL 中选择值时出错
【发布时间】:2011-04-06 20:20:36
【问题描述】:

我有datalistddl,用户可以从ddl 中选择值来绑定datalist。当我尝试在用户选择值时在datalist 中找到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();

                }
            }
        }

    } 

【问题讨论】:

    标签: asp.net drop-down-menu datalist


    【解决方案1】:

    DLProduct.EditItemIndex 应该等于 -1,因为没有被编辑

    只需在抛出错误的行前添加if DLProduct.EditItemIndex >= 0

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2018-05-07
      • 2020-04-27
      • 2020-05-24
      相关资源
      最近更新 更多