[英]Accessing asp.net controls of datalist in codebehind
I am trying to access an asp Image tag in my code behind so that I can display a default image if there is no other image present. 我正在尝试访问后面代码中的asp图像标签,以便在没有其他图像的情况下显示默认图像。 The problem that I am having is that I am getting an "Object reference not set to an instance of an object."
我遇到的问题是我收到“对象引用未设置为对象的实例”。 error message.
错误信息。 The aspx page
aspx页面
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MemberProfileList.ascx.cs"
Inherits="UserControls_MemberProfileList" %>
<%--<asp:GridView ID="GridView1" runat="server">
</asp:GridView>--%>
<asp:DataList ID="profilelist" runat="server" RepeatColumns="2" OnDataBinding="profilelist_DataBound" >
<ItemTemplate>
<asp:Image ID="ProfileImage" runat="server" ImageUrl='<%# System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%>' />
<asp:Hyperlink runat="server" NavigateUrl='<%#Link.ToSpecificMemberProfile(Eval("Username").ToString()) %>' Text='<%#HttpUtility.HtmlDecode(Eval("Username").ToString()) %>' />
</ItemTemplate>
</asp:DataList>
The method in the code behind 后面代码中的方法
profilelist.DataSource = myProfileList;
profilelist.DataBind();
Image ProfileImage = profilelist.FindControl("ProfileImage") as Image;
string profilepic = ProfileImage.ImageUrl.ToString();
if (profilepic == null)
{
ProfileImage.Visible = false;
}
Any help is appreciated 任何帮助表示赞赏
The Image will be inside an Item, not on the whole List. 图片将在项目内,而不是在整个列表内。 Implement your logic inside the ItemDataBound event.
在ItemDataBound事件中实现您的逻辑。
Check this out: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx 检查一下: http : //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx
Regards 问候
//if you're trying to hide it base on weather the ProfilePictureThumb is Null by putting:** //如果您要根据天气将其隐藏,则ProfilePictureThumb为Null,方法是:**
<asp:Image ID="ProfileImage" Visible='<%# String.IsNullOrEmpty(Eval("ProfilePictureThumb").ToString())? false:true %>' runat="server" ImageUrl='<%# System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.