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.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Caline
  • 175
  • 1
  • 1
  • 7

6 Answers6

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
        }    
    }
}
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Mcbeev
  • 1,519
  • 9
  • 9
  • 4
    Is there any way to do this in the databound method? I'd rather *not* hardcode "controls[0]" as that's sloppy. – Broam Feb 10 '10 at 20:57
  • 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. – The Muffin Man Jun 15 '11 at 23:53
  • @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. – Mcbeev Jun 17 '11 at 11:57
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
                }
            }
        }
avani gadhai
  • 460
  • 2
  • 12
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";
}
Stuart
  • 671
  • 7
  • 20
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";
    }
}
mhenry1384
  • 7,538
  • 5
  • 55
  • 74
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

...

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).

Thierry_S
  • 1,526
  • 16
  • 25