访问DataList控件的asp.net控制在codebehind [英] Accessing asp.net controls of datalist in codebehind

查看:97
本文介绍了访问DataList控件的asp.net控制在codebehind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的code访问一个asp图片标签后面,这样如果没有其他图像present我可以显示默认图像。 ,我遇到的问题是,我得到一个对象引用不设置到对象的实例。错误信息。
ASPX页面

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. 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

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

在code的方法背后

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

任何帮助是pciated AP $ P $

Any help is appreciated

推荐答案

图片将是一个项目里面,而不是整个列表。
实现逻辑ItemDataBound事件中。

The Image will be inside an Item, not on the whole List. Implement your logic inside the ItemDataBound event.

检查了这一点:的http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx

问候

这篇关于访问DataList控件的asp.net控制在codebehind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆