Find control in ListView EmptyDataTemplate
17,749
Solution 1
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
}
}
}
Solution 2
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
}
}
}
Solution 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";
}
}
Solution 4
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";
}
Solution 5
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
...
Author by
Caline
Updated on June 15, 2022Comments
-
Caline almost 2 years
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
returnsnull
. I’d like to change the text of theLiteral
control but I don’t have no idea how to do it. -
Broam over 14 yearsIs there any way to do this in the databound method? I'd rather not hardcode "controls[0]" as that's sloppy.
-
The Muffin Man almost 13 yearsif 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. -
ramr almost 13 years@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.
-
JonathanWolfson over 10 yearsThis'll spot the EmptyItemTemplate. There's also an EmptyDataTemplate which this will not see.