为什么方法FindControl()
返回null
直到FormView
,直到调用DataBind()
。FormView.FindControl()返回null直到DataBind()
之后,它正确地返回一切?
有什么解决方法?
致电DataBind()
第一次致电FindControl()
之前?
为什么方法FindControl()
返回null
直到FormView
,直到调用DataBind()
。FormView.FindControl()返回null直到DataBind()
之后,它正确地返回一切?
有什么解决方法?
致电DataBind()
第一次致电FindControl()
之前?
要么显式调用DataBind(),要么将代码放在FormView的DataBound事件中。
FormView
在创建任何数据之前如何获得有关其内容的任何信息?
所以我想你已经回答了你自己的问题,你必须在DataBind()
之前。
它与BINDING无关。一种是寻找服务器控制,而不是其绑定数据。 SO控制应该可以通过FindControl获得。原因是在别的地方...
这确实是FormView.NamingContainer的众所周知的行为。 – abatishchev 2010-11-13 06:31:48
这是非常奇怪的。没有为我调用DataBind()。我不得不创建一个新的列表,添加一个项目,设置为数据源,然后设置数据库。
List<Item> dummyList = new List<Item>();
dummyList.Add(new Item());
formview.DataSource = dummyList;
formview.DataBind();
什么我经历是这样的,
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";
}
//}
}
如何是一个访问控制数据绑定()函数,如果它不是可用,直到发生后,数据绑定?我在OnDataBound回调中,但FindControl返回null。 – Barry 2017-09-20 21:04:08