ASP.NET control in GridView not found to exist in code behind

前端 未结 3 1544
梦毁少年i
梦毁少年i 2021-01-28 12:08

I have a DropDownList that I would like to populate with column values from a DataBase. However, when I try to bind the DropDownList in code behind, the IDE keeps telling me:

3条回答
  •  清酒与你
    2021-01-28 12:42

    your dropdown is in gridview so you can try with this code

    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = (DropDownList)e.Row.FindControl("EqpCatDDL'");
        SqlCommand cmd = new SqlCommand("SELECT EqpCateID, EqpCat FROM EqpCategory", connection);
    
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
    
        EqpCatDDL.DataSource = ds;
        EqpCatDDL.DataValueField = "EqpCateID";
        EqpCatDDL.DataTextField = "EqpCat";
        EqpCatDDL.DataBind();
    
    }
    }
    

提交回复
热议问题