Using a recursive find method, I have tried all day to find "lblName" below so I can add text property to it. Also tried all page lifecycle events; nothing. Master page follows. Find method found "Main Content" but not the label contained in it. Pretty standard
stuff but it has me baffled. Any help would be appreciated.
protected void Page_Load(object sender, EventArgs e)
{
//you can do this
lblName.Text = "foo";
//or you can do this
((Label)Master.FindControl("MainContent").FindControl("lblName")).Text = "foo2";
}
Thanks, but same error message. It's just not finding lblName. I even put in a panel; the find method found the panel just fine but not the label in the page or in the panel.
I think you should try - this.Master.FindControl("lblName")
Or else, try looking your HTML source of your page in browser, and see what ID it generates for "lblName". That will give you idea of hierarchy. If you face any issues, please post the generated ID of lblName here.
I think you should move your controls out of ContentPlaceHolder.
ContentPlaceHolder is basically for child page's controls. I doubt any controls you put directly ContentPlaceHolder within master page are even getting rendered?
Do you see the other controls Price, and all while the page gets opened in browser?
muybn
Participant
853 Points
1290 Posts
can't find label
May 05, 2012 09:25 PM|LINK
Using a recursive find method, I have tried all day to find "lblName" below so I can add text property to it. Also tried all page lifecycle events; nothing. Master page follows. Find method found "Main Content" but not the label contained in it. Pretty standard stuff but it has me baffled. Any help would be appreciated.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Checkout.aspx.cs" Inherits="Checkout" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <div style="width: 900px; text-align: center;"> <asp:Label ID="lblCheckout" Font-Size="Large" Text="Thank you for shopping. Order details:" runat="server" /><br /> <asp:Label ID="lblName" Font-Size="Large" runat="server" /> <asp:Label ID="lblPrice" Font-Size="Large" runat="server">$100</asp:Label> </div> </asp:Content><%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head runat="server"> <title></title> <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /> <asp:ContentPlaceHolder ID="HeadContent" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form runat="server"> <div class="page"> <div class="header"> <div class="title"> <h1> Pay 41 Demo </h1> </div> <div class="loginDisplay"> <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false"> <AnonymousTemplate> [ <a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a> ] </AnonymousTemplate> <LoggedInTemplate> Welcome <span class="bold"> <asp:LoginName ID="HeadLoginName" runat="server" /> </span>! [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/" /> ] </LoggedInTemplate> </asp:LoginView> </div> <div class="clear hideSkiplink"> <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> <Items> <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" /> <asp:MenuItem NavigateUrl="~/Products.aspx" Text="Products" /> <asp:MenuItem NavigateUrl="~/Checkout.aspx" Text="Checkout" /> </Items> </asp:Menu> </div> </div> <div class="main"> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </div> <div class="clear"> </div> </div> <div class="footer"> </div> </form> </body> </html>mbanavige
All-Star
134950 Points
15415 Posts
ASPInsiders
Moderator
MVP
Re: can't find label
May 05, 2012 09:55 PM|LINK
try these:
protected void Page_Load(object sender, EventArgs e) { //you can do this lblName.Text = "foo"; //or you can do this ((Label)Master.FindControl("MainContent").FindControl("lblName")).Text = "foo2"; }muybn
Participant
853 Points
1290 Posts
Re: can't find label
May 06, 2012 06:06 AM|LINK
Thanks, but same error message. It's just not finding lblName. I even put in a panel; the find method found the panel just fine but not the label in the page or in the panel.
ZeeshanAnsar...
Participant
878 Points
264 Posts
Re: can't find label
May 06, 2012 06:25 AM|LINK
lblName is placed in your content page
so how you are accessing the label can you post your
code?
Please 'Mark as Answer' if this post helps you.
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: can't find label
May 06, 2012 06:28 AM|LINK
Hi
try this in your master page
since your control inside contentplaceholder so
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder) Page.Master.FindControl("MainContent");
Label lblName= (Label) ContentPlaceHolder1.FindControl("lblName");
nirman.doshi
Participant
1520 Points
775 Posts
Re: can't find label
May 06, 2012 06:35 AM|LINK
I think you should try - this.Master.FindControl("lblName")
Or else, try looking your HTML source of your page in browser, and see what ID it generates for "lblName". That will give you idea of hierarchy. If you face any issues, please post the generated ID of lblName here.
Software Developer
Vadodara, India
basheerkal
Star
10672 Points
2426 Posts
Re: can't find label
May 06, 2012 08:22 AM|LINK
I cannot believe it
even without using FindControl it should find the label.
Just lblName.Text = "foo"; should populate the label.
In which .cs file You write the code?
(Talk less..Work more)
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: can't find label
May 06, 2012 08:25 AM|LINK
how lblName.text should work??
as i understand that he is trying to access child control from master page
and lblName is in child
nirman.doshi
Participant
1520 Points
775 Posts
Re: can't find label
May 06, 2012 09:28 AM|LINK
No, i think "lblName" is placed in master page, and he is trying to access it from the child page.
Software Developer
Vadodara, India
nirman.doshi
Participant
1520 Points
775 Posts
Re: can't find label
May 06, 2012 09:45 AM|LINK
I think you should move your controls out of ContentPlaceHolder.
ContentPlaceHolder is basically for child page's controls. I doubt any controls you put directly ContentPlaceHolder within master page are even getting rendered?
Do you see the other controls Price, and all while the page gets opened in browser?
Software Developer
Vadodara, India