jn1480
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?
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>
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
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.NullReferenceExcept
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>
Would u try pre_render event
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>
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 ).FindCont rol("lbl") {Text = "aaaa"} System.Web.UI.Control
and what i got in my watch window when i put a breakepoint on prerendr event of the listview
+ itemPlaceholder.Controls(0
<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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
thanks!!
then
in this event code
ctype(listview1.findcontro
Melih SARICA