在代码隐藏中访问datalist的asp.net控件

我试图在后面的代码中访问asp图像标记,以便在没有其他图像存在的情况下显示默认图像。 我遇到的问题是我得到一个“对象引用未设置为对象的实例”。 错误信息。 aspx页面

 <%-- --%>   <asp:Image ID="ProfileImage" runat="server" ImageUrl='' /> <asp:Hyperlink runat="server" NavigateUrl='' Text='' />   

代码背后的方法

 profilelist.DataSource = myProfileList; profilelist.DataBind(); Image ProfileImage = profilelist.FindControl("ProfileImage") as Image; string profilepic = ProfileImage.ImageUrl.ToString(); if (profilepic == null) { ProfileImage.Visible = false; } 

任何帮助表示赞赏

图像将位于项目内,而不是整个列表中。 在ItemDataBound事件中实现您的逻辑。

看看这个: http : //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx

问候

//如果你试图根据天气隐藏它,ProfilePictureThumb是空的:放**:**

   //if you want to make it display a default image based on weather or not ProfilePicture is null: ImageUrl='<%# String.IsNullOrEmpty(Eval("ProfilePictureThumb").ToString())? System.String.Format("/Images/{0}", "defaultimage.jpg"):System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%>' //that way you don't need code behind.