开发者

error when bind datalist with value from ddl

开发者 https://www.devze.com 2023-02-23 01:03 出处:网络
I had ddl in datalist and when I tried to bind datalist with value from ddl this error apeared (object refrence not set ...) here DDLProduct.SelectedIndex

I had ddl in datalist and when I tried to bind datalist with value from ddl this error apeared (object refrence not set ...) here DDLProduct.SelectedIndex

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)
        {
            D开发者_开发知识库DLProduct = 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();

            }
        }
    }


I think your problem is here:

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

Whats the purpose of this line?

You already have a reference to this control, "DDLProduct"

edit - without the actual exception ect I'm only guessing.


What line gives you the error?

Just a guess but try

DDLProduct = sender as DropDownList;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号