Link to home
Start Free TrialLog in
Avatar of jn1480
jn1480Flag for United States of America

asked on

Set Label value within Listview template from codebehind

We have a listview control with the EmptyDataTemplate containing a Label control that we would like to set the text of from the codebehind file.

However, it appears if your label control is INSIDE a listview control, it is not readily accessible via the normal label.text = "xyz" method we are used to.

What am I missing?
<EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            <asp:Label ID="lvPlans_EmptyData" runat="server" Text="" Visible="True"></asp:Label></td>
                    </tr>
                </table>
            </EmptyDataTemplate>

Open in new window

Avatar of Melih SARICA
Melih SARICA
Flag of Türkiye image

Handle On item created event

then
 in this event code

 ctype(listview1.findcontrol("lvPlans_EmptyData"),label).text="There u go "

Melih SARICA
Avatar of jn1480

ASKER

Thanks Melih - but it errors out:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
 

    Protected Sub lvPlans_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvPlans.ItemCreated
        CType(lvPlans.FindControl("lvPlans_EmptyData"), Label).Text = "There u go"
        'lvPlans.FindControl("lvPlans_EmptyData") = "there u go"
    End Sub
 
 
 
 
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            <asp:Label ID="lvPlans_EmptyData" runat="server"></asp:Label>
                            </td>
                    </tr>
                </table>
            </EmptyDataTemplate>

Open in new window

Would u try pre_render event
Avatar of jn1480

ASKER

Still no luck...

    Protected Sub lvPlans_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvPlans.PreRender
        CType(lvPlans.FindControl("lvPlans_EmptyData"), Label).Text = "There u go"
        'lvPlans.FindControl("lvPlans_EmptyData") = "there u go"
    End Sub
 
 
 
 
 
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            <asp:Label ID="lvPlans_EmptyData" runat="server"></asp:Label>
                            </td>
                    </tr>
                </table>
            </EmptyDataTemplate>

Open in new window

what i did try simple is this

and what i got in my watch window when i put a breakepoint on prerendr event of the listview

+            itemPlaceholder.Controls(0).FindControl("lbl")      {Text = "aaaa"}      System.Web.UI.Control

<asp:ListView runat="server" DataSourceID="XmlDataSource1" ID="itemPlaceholder">
            <EmptyDataTemplate>
                <asp:Label runat="server" ID="lbl">aaaa</asp:Label>
            </EmptyDataTemplate>
            <ItemTemplate>
                <asp:Label runat="server" ID="lbl"><%#XPath("./Name")%></asp:Label>
            </ItemTemplate>
            <LayoutTemplate>
                <a href ="" >deneme</a>
                <panel runat="server" id="itemPlaceholder" />
 
            </LayoutTemplate>
        </asp:ListView>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml" XPath="/book">
        </asp:XmlDataSource>
 
 
Protected Sub itemPlaceholder_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles itemPlaceholder.PreRender
        Dim a As New Label
    End Sub

Open in new window

Avatar of jn1480

ASKER

I'm sorry Melih... perhaps I am just too much of a newbie, but I am not sure what you said in your last post?
ASKER CERTIFIED SOLUTION
Avatar of Melih SARICA
Melih SARICA
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jn1480

ASKER

Perfect - thanks again!!

    Protected Sub lvPlans_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvPlans.PreRender
        If Page.IsPostBack and  Then
           CType(lvPlans.Controls(0).FindControl("lvPlans_emptyData"), Label).Text = "No plans in this category..."
      End If
    End Sub

Open in new window

Avatar of jn1480

ASKER

thanks!!