Find control in ListView EmptyDataTemplate
Asked Answered
Q

6

12

I have the a ListView like this

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

In Page_Load() I have the following:

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

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.

Quadratic answered 5/3, 2009 at 1:28 Comment(0)
Y
21

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.

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
        }    
    }
}
Yousuf answered 5/3, 2009 at 2:5 Comment(3)
Is there any way to do this in the databound method? I'd rather not hardcode "controls[0]" as that's sloppy.Compeer
if I take out .Controls[0] I get an error. Can you help me understand why you need it? It seems like we're telling it the index of the control and the name, I fail to see why both would be necessary.Magus
@Nick .Controls[0] is a reference to the EmptyDataTemplate object. After you have a reference to that object, you are looking for a control called "Literal1" in the child controls of EmptyDataTemplate.Yousuf
K
4

You can use the following:

 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
                }
            }
        }
Kellsie answered 4/5, 2012 at 13:7 Comment(1)
This'll spot the EmptyItemTemplate. There's also an EmptyDataTemplate which this will not see.Spiral
F
3

It's not specifically what you asked, but another way to do that kind of thing is like this:

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

where Foobar is defined in your page's code behind file

public partial class MyClass : System.Web.UI.Page
{
...
    public string Foobar()
    {
         return "whatever";
    }
}
Fighterbomber answered 16/2, 2011 at 17:58 Comment(0)
N
3

An alternative approach...

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

In code-behind...

protected void Literal1_Init(object sender, EventArgs e)
{
    (sender as Literal).Text = "Some other text";
}
Nodule answered 17/9, 2013 at 14:45 Comment(0)
S
1
 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

...

Simms answered 24/8, 2012 at 12:59 Comment(0)
K
1

Answering Broam's question "Is there any way to do this in the databound method? I'd rather not hardcode "controls[0]" as that's sloppy"

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

Unfortunately, I've not found a way to not use Controls[0].

In the usual Item events (ItemDataBound or ItemCreate), you can use e.Item of the ListViewItemEventArgs to get the ListViewItem. In the DataBound event, there's only a generic EventArgs.

And on top of that, it seems that ((Control)sender).FindControl("Literal1") doesn't work either (find control from the listview at the top of the tree), hence the use of Controls[0] .FindControl(...) (find control from the listviewitem).

Kramer answered 18/2, 2014 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.