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

前端 未结 3 1542
梦毁少年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:41

    Use this code to bound data to dropdown without using RowDataBound.

    Create a function that'll bind the Data to dropdown as follow and call it in Page_Load event

    Public void fill_gridView_dropDown()
    {
        // your connection and query to retrieve dropdown data will go here 
        // this loop will go through all row in GridView
        foreach(GridViewRow row in your_gridView_Name.Rows) {
            DropDownList  dropDown = (DropDownList)row.FindControl("dropDownList_id");
            dropDown.DataSource = dataSource;
            dropDown.DataValueField = "ValueField";
            dropDown.DataTextField = "TextField";
            dropDown.DataBind();
        }
    }
    

    Please note that you have to bind GridView first, and then you have to bind your dropdown

提交回复
热议问题