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

前端 未结 3 1539
梦毁少年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 13:06

    You can't directly populate GridView's dropdownlist like this. You need to set datasource of GridView first i.e.

    GridView1.DataSource = DataSource
    

    And if you would like to access dropdownlist of this gridview, you can use RowDataBound event handler of GridView i.e.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Checking whether the Row is Data Row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Finding the Dropdown control.
            Control ctrl = e.Row.FindControl("EqpCatDDL");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                List lst = new List();
                dd.DataSource = lst;
                dd.DataBind();
            }
        }
    }
    

提交回复
热议问题