FormView.FindControl() returns null until DataBind()

后端 未结 5 2027
轻奢々
轻奢々 2020-12-19 23:24

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

After that it returns everything correct

5条回答
  •  再見小時候
    2020-12-19 23:53

    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)
            {
                LoadBookmarkImage();
            }
      private void LoadBookmarkImage()
            {
                //if (_swapDetails != null)
                //{             
                    try
                    {
                        _currnetSession = new SessionBL(_user);
    
                        List _tmp = null;
                        string sample = Convert.ToString(Page.RouteData.Values["sessionCode"]);
                        if (Session["Prefernce"] != null)
                        {
                            _tmp = (List)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 _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";
                    }
                //}
            } 
    

提交回复
热议问题