Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

...

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top