2012-06-29 91 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. 
相關問題