我想在後面的代碼中訪問一個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;
}
任何幫助表示讚賞
由於它的工作就像魅力 – tuckerjt07
你會介意投票給這個答案嗎? =) –