0

他の画像が存在しない場合にデフォルトの画像を表示できるように、コード ビハインドで asp Image タグにアクセスしようとしています。私が抱えている問題は、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というメッセージが表示されることです。エラーメッセージ。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>

コードビハインドのメソッド

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

どんな助けでも大歓迎です

4

2 に答える 2

1

画像はリスト全体ではなく、アイテム内に表示されます。ItemDataBoundイベント内にロジックを実装します。

これをチェックしてください:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx

よろしく

于 2012-06-29T17:24:29.867 に答える
0

//天候に基づいて非表示にしようとしている場合は、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. 
于 2012-06-29T19:06:54.153 に答える