2012-06-29 55 views
0

我想在后面的代码中访问一个asp图像标签,这样如果没有其他图像存在,我可以显示默认图像。我遇到的问题是我得到的“对象引用未设置为对象的实例”。错误信息。 ASPX页面在代码隐藏中访问datalist的asp.net控件

<%@ 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> 

代码的方法背后

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

任何帮助表示赞赏

回答

0

//如果你想通过把隐藏它的天气ProfilePictureThumb是空基地:**

<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. 
相关问题