FormView.FindControl() returns null until DataBind()

Why method FindControl() returns null on FormView until call DataBind().

After that it returns everything correctly?

What workaround are there?

Call DataBind() before first call of FindControl() ?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Either explicitly call DataBind(), or place your code in the DataBound event of the FormView.

Method 2

How would a FormView have any information about its content before it has any data to build it upon?

So I guess you already answered your own question, you will have to DataBind() before.

Method 3

It has nothing to do with BINDING. One is looking for SERVER CONTROL, not for ITS BOUND DATA. SO – control should be available via FindControl. The reason is somewhere else…

Method 4

It is very odd. Did not work for me just calling DataBind(). I had to create a new List, add an item, set as datasource, then databin.

List<Item> dummyList = new List<Item>();
dummyList.Add(new Item());
formview.DataSource = dummyList;

Method 5

what I experienced is this,

System.Web.UI.HtmlControls.HtmlImage bookmarkload = sessionDetail.FindControl("bookmarkimage") as System.Web.UI.HtmlControls.HtmlImage;

returned null.

So, I did this:

 protected void sessionDetail_DataBound(object sender, EventArgs e)
  private void LoadBookmarkImage()
            //if (_swapDetails != null)
                    _currnetSession = new SessionBL(_user);

                    List<SessionVO> _tmp = null;
                    string sample = Convert.ToString(Page.RouteData.Values["sessionCode"]);
                    if (Session["Prefernce"] != null)
                        _tmp = (List<SessionVO>)Session["Prefernce"];
                    if (_tmp != null && _tmp.Count > 0)
                        _tmp = _tmp.Where(p => p.SessionCode == sample).ToList();

                    //_currentFavorite.SessionD = _swapDetails[0];
                    _currentFavorite.SessionD = _tmp[0];

                    List<FavoriteVO> _swapList = _user.ViewFavoriteONID(_currentFavorite.SessionD.SessionID);

                    if (_swapList != null && _swapList.Count > 0)
                        //access javascript counter variable
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "counter=1;", true);
                        System.Web.UI.HtmlControls.HtmlImage bookmarkload = sessionDetail.FindControl("bookmarkimage") as System.Web.UI.HtmlControls.HtmlImage;
                        bookmarkload.Src = "/Images/heart-checked.png";
                catch (Exception ex)
                    labelinfo.Visible = true;
                    labelinfo.InnerHtml = ex.Message;
                    labelinfo.Attributes["class"] = "centering text-center text-danger";

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x