首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

很奇怪的现象:嵌套了母板页的页面,其中DataList中控件隐藏的有关问题(大家帮个忙)

2011-12-26 
很奇怪的现象:嵌套了母板页的页面,其中DataList中控件隐藏的问题(大家帮个忙) 大概意思是这样:前台DataLis

很奇怪的现象:嵌套了母板页的页面,其中DataList中控件隐藏的问题(大家帮个忙)

大概意思是这样:前台DataList   中的Label控件 "labIsPerfect "值为1时   ,则图片显示,为0,则图片隐藏

(1)前台页面   .aspx

<asp:DataList   ID= "dlPost "   runat= "server "   Width= "855px ">
                        <ItemTemplate>
                        <asp:Image   runat= "server "   ID= "imgOK "   ImageUrl= "~/Images/jing.gif "   />
                        <asp:Label   ID= "labIsPerfect "   runat= "server "   Text= ' <%#   Eval( "IsPerfect ")   %> '   > </asp:Label>
                        </ItemTemplate>
                </asp:DataList>

(2)后台的.cs中
    protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                if   (!IsPostBack)
                {
                        TopicBind();
                }
        }

  private   void   TopicBind()
        {
                //此处省略,绑定数据等
                dlPost.DataSource   =   dt;
                dlPost.DataBind();

                foreach   (DataListItem   item   in   aa.Items)
                {
                        Image   imgOK   =   (Image)item.FindControl( "imgOK ");
                        Label   labIsPerfect   =   (Label)item.FindControl( "labIsPerfect ");
                        if   (Convert.ToInt32(labIsPerfect.Text)   ==   1)
                        {
                                imgOK.Visible   =   true;
                        }
                        else
                        {
                                imgOK.Visible   =   false;
                        }
                }
        }

代码应该没有什么问题,
在不嵌套母板页的页面中,显示正常(但我的页面肯定是要放在母板页中的)

但在嵌套了母板的页面中,image不正常,好像没有找到img,所以图片还是显示出来的
我后来把foreach()这段代码放在DataList   的事件   OnItemDataBound   中,图片能显示出来,除了Datalist最后一行,因为最后一行的   labIsPerfect值是0,但图片确还是能显示出来.




大概意思就是这样,不知道各位听懂没有,我想了好长事件还没搞定,
太影响我的转正和今后在公司的发展了,大家帮个忙吧.搞定了就立刻给分!



[解决办法]
在DataList的ItemDataBound事件中:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image imgOK = (Image)item.FindControl( "imgOK ");
Label labIsPerfect = (Label)item.FindControl( "labIsPerfect ");
if (Convert.ToInt32(labIsPerfect.Text) == 1)
{
imgOK.Visible = true;
}
else
{
imgOK.Visible = false;
}

}

[解决办法]
先查找母模板中的 ContentPlaceHolder 控件,
ContentPlaceHolder cph = Master.FindControl( "ContentPlaceHolder1 ") as ContentPlaceHolder;
再从 ContentPlaceHolder 控件里查找 DataList 控件
if (cph != null)
DataList dl = cph.FindControl( "aa ") as DataList;

热点排行