【问题标题】:Find control in ListView EmptyDataTemplate在 ListView EmptyDataTemplate 中查找控件
【发布时间】:2010-10-11 10:15:21
【问题描述】:

我有一个像这样的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 返回null。我想更改Literal 控件的文本,但我不知道该怎么做。

【问题讨论】:

    标签: c# asp.net .net listview findcontrol


    【解决方案1】:

    回答 Broam 的问题“有没有办法在数据绑定方法中做到这一点?我不想硬编码“controls[0]”,因为那太草率了”

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        ListView mylist = ((ListView)sender);
        ListViewItem lvi = null;
        if (mylist.Controls.Count == 1)
            lvi = mylist.Controls[0] as ListViewItem;
    
        if (lvi == null || lvi.ItemType != ListViewItemType.EmptyItem)
            return;
    
        Literal literal1 = (Literal)lvi.FindControl("Literal1");
        if (literal1 != null)
            literal1.Text = "No items to display";
    }
    

    很遗憾,我还没有找到不使用 Controls[0] 的方法。

    在通常的Item事件(ItemDataBound或ItemCreate)中,可以使用ListViewItemEventArgs的e.Item来获取ListViewItem。在 DataBound 事件中,只有一个通用的 EventArgs。

    最重要的是,似乎 ((Control)sender).FindControl("Literal1") 也不起作用(从树顶部的列表视图中查找控件),因此使用 Controls[ 0] .FindControl(...) (从列表视图项中查找控件)。

    【讨论】:

      【解决方案2】:

      我相信除非你在代码后面的某处调用你的ListViewDataBind 方法,否则ListView 永远不会尝试数据绑定。然后什么都不会渲染,甚至Literal控件也不会被创建。

      在您的 Page_Load 活动中尝试类似:

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

      【讨论】:

      • 在数据绑定方法中有没有办法做到这一点?我宁愿硬编码“controls[0]”,因为那太草率了。
      • 如果我取出 .Controls[0] 我得到一个错误。你能帮我理解你为什么需要它吗?似乎我们在告诉它控件的索引和名称,我不明白为什么两者都是必要的。
      • @Nick .Controls[0] 是对 EmptyDataTemplate 对象的引用。获得对该对象的引用后,您将在 EmptyDataTemplate 的子控件中查找名为“Literal1”的控件。
      【解决方案3】:

      另一种方法...

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

      在代码隐藏中...

      protected void Literal1_Init(object sender, EventArgs e)
      {
          (sender as Literal).Text = "Some other text";
      }
      

      【讨论】:

        【解决方案4】:
         Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
            Dim searchValue As String = Replace(Request.QueryString("s"), "", "'")
            Dim searchLiteral2 As Literal = CType(ListView1.FindControl("Literal2"), Literal)
            searchLiteral2.Text = "''" & searchValue & "''"
        End Sub
        

        ...

        【讨论】:

          【解决方案5】:

          您可以使用以下内容:

           protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
                  {
                      if (e.Item.ItemType == ListViewItemType.EmptyItem)
                      {
                           Control c = e.Item.FindControl("Literal1");
                          if (c != null)
                          {
                              //this will atleast tell you  if the control exists or not
                          }
                      }
                  }
          

          【讨论】:

          • 这会发现 EmptyItemTemplate。还有一个 EmptyDataTemplate 这不会看到。
          【解决方案6】:

          这不是你问的具体问题,但另一种做这种事情的方法是这样的:

          <EmptyDataTemplate>
            <%= Foobar() %>
          </EmptyDataTemplate>
          

          Foobar 在页面代码隐藏文件中的定义

          public partial class MyClass : System.Web.UI.Page
          {
          ...
              public string Foobar()
              {
                   return "whatever";
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-07
            • 1970-01-01
            相关资源
            最近更新 更多