【问题标题】:FormView.FindControl() returns null until DataBind()FormView.FindControl() 在 DataBind() 之前返回 null
【发布时间】:2011-01-11 09:38:06
【问题描述】:

为什么方法FindControl()FormView 上返回null 直到调用DataBind()

之后它会正确返回所有内容吗?

有什么解决方法?

在第一次拨打FindControl()之前先拨打DataBind()

【问题讨论】:

    标签: .net asp.net asp.net-2.0 formview findcontrol


    【解决方案1】:

    要么显式调用 DataBind(),要么将代码放在 FormView 的 DataBound 事件中。

    【讨论】:

      【解决方案2】:

      FormView 在有任何数据可以构建它之前如何获得有关其内容的任何信息?

      所以我猜你已经回答了你自己的问题,你必须先DataBind()

      【讨论】:

      • 如果 DataBound() 函数中的控件在数据绑定发生后才可用,如何访问该控件?我在 OnDataBound 回调中,但 FindControl 返回 null。
      【解决方案3】:

      它与 BINDING 无关。一个是寻找服务器控制,而不是它的绑定数据。 SO - 控制应该可以通过 FindControl 获得。原因在别的地方……

      【讨论】:

      • 这确实是 FormView.NamingContainer 的众所周知的行为。
      【解决方案4】:

      这很奇怪。仅调用 DataBind() 对我不起作用。我必须创建一个新列表,添加一个项目,设置为数据源,然后是数据仓。

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

      【讨论】:

        【解决方案5】:

        我的经历是这样的,

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

        返回null

        所以,我这样做了:

         protected void sessionDetail_DataBound(object sender, EventArgs e)
                {
                    LoadBookmarkImage();
                }
          private void LoadBookmarkImage()
                {
                    //if (_swapDetails != null)
                    //{             
                        try
                        {
                            _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";
                        }
                    //}
                } 
        

        【讨论】:

          猜你喜欢
          • 2020-09-08
          • 2016-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-02
          相关资源
          最近更新 更多