Asp.net 从DDL中选择值时出错
我有Asp.net 从DDL中选择值时出错,asp.net,drop-down-menu,datalist,Asp.net,Drop Down Menu,Datalist,我有datalist和ddl,用户可以从ddl中选择值绑定datalist。当用户选择值时,我试图在datalist中查找ddl时,出现了此错误 索引超出范围。必须是 非负数且小于 收藏。参数名称:索引 这里 所以请任何人帮助我 private DropDownList DDLProduct2; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {
datalist
和ddl
,用户可以从ddl
中选择值绑定datalist
。当用户选择值时,我试图在datalist
中查找ddl
时,出现了此错误
索引超出范围。必须是
非负数且小于
收藏。参数名称:索引
这里
所以请任何人帮助我
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();
}
}
}
}
DLProduct.EditItemIndex应等于-1,因为未编辑任何内容 如果DLProduct.EditItemIndex>=0,只需在抛出错误的行之前添加一个
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();
}
}
}
}