C# 在ListView EmptyDataTemplate中查找控件

C# 在ListView EmptyDataTemplate中查找控件,c#,asp.net,.net,listview,findcontrol,C#,Asp.net,.net,Listview,Findcontrol,我有这样的aListView <asp:ListView ID="ListView1" runat="server"> <EmptyDataTemplate> <asp:Literal ID="Literal1" runat="server" text="some text"/> </EmptyDataTemplate> ... </asp:ListView> 但是x返回null。我想更改Literal控

我有这样的a
ListView

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

但是
x
返回
null
。我想更改
Literal
控件的文本,但我不知道如何操作。

我相信,除非调用
ListView
中的
DataBind
方法,否则
ListView
将永远不会尝试数据绑定。然后将不会呈现任何内容,甚至不会创建
Literal
控件

页面\u 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
        }    
    }
}

这不是你具体要求的,但另一种方法是:

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

您可以使用以下选项:

Literal x = (Literal)ListView1.FindControl("Literal1");
x.Text = "other text";
 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
                }
            }
        }

…另一种方法

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

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

不幸的是,我没有找到不使用控件的方法[0]

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


除此之外,似乎((Control)sender).FindControl(“Literal1”)也不起作用(从树顶部的listview中查找控件),因此使用了控件[0]。FindControl(…)(从listviewitem中查找控件)。

在数据绑定方法中有什么方法可以做到这一点吗?我不想硬编码“controls[0]”,因为这太草率了。如果我去掉
。controls[0]
我会出错。你能帮我理解你为什么需要它吗?我们似乎在告诉它控件的索引和名称,我不明白为什么两者都是必需的。@Nick.Controls[0]是对EmptyDataTemplate对象的引用。对该对象进行引用后,将在EmptyDataTemplate的子控件中查找名为“Literal1”的控件。这将发现EmptyItemTemplate。还有一个EmptyDataTemplate,这将不会看到。
 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
<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";
}
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";
}