查找的ListView EmptyDataTemplate控制 [英] Find control in ListView EmptyDataTemplate

查看:192
本文介绍了查找的ListView EmptyDataTemplate控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 像这样

<asp:ListView ID="ListView1" runat="server">
   <EmptyDataTemplate>
      <asp:Literal ID="Literal1" runat="server" text="some text"/>
   </EmptyDataTemplate>
   ...
</asp:ListView>

的Page_Load()我有以下几点:

Literal x = (Literal)ListView1.FindControl("Literal1");
x.Text = "other text";

X 返回。 I&rsquo的;倒要修改文字控件的文本,但我不和rsquo的;吨有不知道该怎么做。

but x returns null. I’d like to change the text of the Literal control but I don’t have no idea how to do it.

推荐答案

我认为,除非你调用的的DataBind 方法,你的的ListView 在身后code某处时,的ListView 将永远不会尝试将数据绑定。那么什么都不会渲染,甚至文字控制荣获&rsquo的; T为创建

I believe that unless you call the DataBind method of your ListView somewhere in code behind, the ListView will never try to data bind. Then nothing will render and even the Literal control won’t be created.

在你的的Page_Load 事件试着这么做:

In your Page_Load event try something like:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //ListView1.DataSource = ...
        ListView1.DataBind();

        //if you know its empty empty data template is the first parent control
        // aka Controls[0]
        Control c = ListView1.Controls[0].FindControl("Literal1");
        if (c != null)
        {
            //this will atleast tell you  if the control exists or not
        }    
    }
}

这篇关于查找的ListView EmptyDataTemplate控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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