can't find labelhttp://forums.asp.net/t/1800526.aspx/1?can+t+find+labelSun, 06 May 2012 17:07:06 -040018005264967267http://forums.asp.net/p/1800526/4967267.aspx/1?can+t+find+labelcan't find label <p>Using a recursive find method, I have tried all day to find &quot;lblName&quot; below so I can add text property to it. Also tried all page lifecycle events; nothing. Master page follows. Find method found &quot;Main Content&quot; but not the label contained in it. Pretty standard stuff but it has me baffled. Any help would be appreciated.</p> <pre class="prettyprint">&lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.master&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Checkout.aspx.cs&quot; Inherits=&quot;Checkout&quot; %&gt; &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;HeadContent&quot; runat=&quot;Server&quot;&gt; &lt;/asp:Content&gt; &lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;Server&quot;&gt; &lt;div style=&quot;width: 900px; text-align: center;&quot;&gt; &lt;asp:Label ID=&quot;lblCheckout&quot; Font-Size=&quot;Large&quot; Text=&quot;Thank you for shopping. Order details:&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt; <strong>&lt;asp:Label ID=&quot;lblName&quot; Font-Size=&quot;Large&quot; runat=&quot;server&quot; /&gt;</strong> &lt;asp:Label ID=&quot;lblPrice&quot; Font-Size=&quot;Large&quot; runat=&quot;server&quot;&gt;$100&lt;/asp:Label&gt; &lt;/div&gt; &lt;/asp:Content&gt;</pre> <p></p> <pre class="prettyprint">&lt;%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /&gt; &lt;asp:ContentPlaceHolder ID="HeadContent" runat="server"&gt; &lt;/asp:ContentPlaceHolder&gt; &lt;/head&gt; &lt;body&gt; &lt;form runat="server"&gt; &lt;div class="page"&gt; &lt;div class="header"&gt; &lt;div class="title"&gt; &lt;h1&gt; Pay 41 Demo &lt;/h1&gt; &lt;/div&gt; &lt;div class="loginDisplay"&gt; &lt;asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false"&gt; &lt;AnonymousTemplate&gt; [ &lt;a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server"&gt;Log In&lt;/a&gt; ] &lt;/AnonymousTemplate&gt; &lt;LoggedInTemplate&gt; Welcome &lt;span class="bold"&gt; &lt;asp:LoginName ID="HeadLoginName" runat="server" /&gt; &lt;/span&gt;! [ &lt;asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/" /&gt; ] &lt;/LoggedInTemplate&gt; &lt;/asp:LoginView&gt; &lt;/div&gt; &lt;div class="clear hideSkiplink"&gt; &lt;asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"&gt; &lt;Items&gt; &lt;asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" /&gt; &lt;asp:MenuItem NavigateUrl="~/Products.aspx" Text="Products" /&gt; &lt;asp:MenuItem NavigateUrl="~/Checkout.aspx" Text="Checkout" /&gt; &lt;/Items&gt; &lt;/asp:Menu&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="main"&gt; &lt;asp:ContentPlaceHolder ID="MainContent" runat="server" /&gt; &lt;/div&gt; &lt;div class="clear"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="footer"&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><br> <br> </p> 2012-05-05T21:25:25-04:004967277http://forums.asp.net/p/1800526/4967277.aspx/1?Re+can+t+find+labelRe: can't find label <p>try these:</p> <pre class="prettyprint">protected void Page_Load(object sender, EventArgs e) { //you can do this lblName.Text = &quot;foo&quot;; //or you can do this ((Label)Master.FindControl(&quot;MainContent&quot;).FindControl(&quot;lblName&quot;)).Text = &quot;foo2&quot;; }</pre> <p><br> <br> </p> 2012-05-05T21:55:35-04:004967416http://forums.asp.net/p/1800526/4967416.aspx/1?Re+can+t+find+labelRe: can't find label <p>Thanks, but same error message. It's just not finding <strong>lblName</strong>. 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.</p> 2012-05-06T06:06:24-04:004967423http://forums.asp.net/p/1800526/4967423.aspx/1?Re+can+t+find+labelRe: can't find label <p>lblName is placed in your content page</p> <p>so how you are accessing the label can you post your</p> <p>code?</p> 2012-05-06T06:25:39-04:004967426http://forums.asp.net/p/1800526/4967426.aspx/1?Re+can+t+find+labelRe: can't find label <p>Hi&nbsp;</p> <p>try this in your master page</p> <p>since your control inside contentplaceholder so</p> <p><strong><span color="#2b91af" size="2" style="color:#2b91af; font-size:small">ContentPlaceHolder</span><span size="2" style="font-size:small">&nbsp;ContentPlaceHolder1 = (</span><span color="#2b91af" size="2" style="color:#2b91af; font-size:small">ContentPlaceHolder</span><span size="2" style="font-size:small">) Page.Master.FindControl(</span><span color="#a31515" size="2" style="color:#a31515; font-size:small">&quot;</span>MainContent<span color="#a31515" size="2" style="color:#a31515; font-size:small">&quot;</span><span size="2" style="font-size:small">);</span></strong></p> <p><strong><span size="2" style="font-size:small"></span></strong></p> <p><strong><span size="2" style="font-size:small"></span><span color="#2b91af" size="2" style="color:#2b91af; font-size:small">Label</span><span size="2" style="font-size:small">&nbsp;</span><span class="atv">lblName</span><span size="2" style="font-size:small">= (</span><span color="#2b91af" size="2" style="color:#2b91af; font-size:small">Label</span><span size="2" style="font-size:small">) ContentPlaceHolder1.FindControl(</span><span color="#a31515" size="2" style="color:#a31515; font-size:small">&quot;lblName&quot;</span><span size="2" style="font-size:small">);</span></strong></p> 2012-05-06T06:28:24-04:004967432http://forums.asp.net/p/1800526/4967432.aspx/1?Re+can+t+find+labelRe: can't find label <p>I think you should try - this.Master.FindControl(&quot;lblName&quot;)</p> <p>Or else, try looking your HTML source of your page in browser, and see what ID it generates for &quot;lblName&quot;. That will give you idea of hierarchy. If you face any issues, please post the generated ID of lblName here.</p> 2012-05-06T06:35:51-04:004967501http://forums.asp.net/p/1800526/4967501.aspx/1?Re+can+t+find+labelRe: can't find label <p>I cannot believe it</p> <p>even without using FindControl it should find the label.</p> <p>&nbsp;Just <strong>lblName<span class="pun">.</span><span class="typ">Text</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="str">&quot;foo&quot;</span></strong><span class="pun"><strong>;</strong> should populate the label.</span></p> <p><span class="pun">In which .cs file You write the code? </span></p> <p><span class="pun"></span></p> <p><span class="pun"></span></p> 2012-05-06T08:22:06-04:004967503http://forums.asp.net/p/1800526/4967503.aspx/1?Re+can+t+find+labelRe: can't find label <p>how lblName.text should work??</p> <p>as i understand that he is trying to access child control from master page&nbsp;</p> <p>and lblName is in child&nbsp;</p> 2012-05-06T08:25:43-04:004967532http://forums.asp.net/p/1800526/4967532.aspx/1?Re+can+t+find+labelRe: can't find label <p>No, i think &quot;lblName&quot; is placed in master page, and he is trying to access it from the child page.</p> <p></p> 2012-05-06T09:28:27-04:004967541http://forums.asp.net/p/1800526/4967541.aspx/1?Re+can+t+find+labelRe: can't find label <p>I think you should move your controls out of ContentPlaceHolder.</p> <p>ContentPlaceHolder is basically for child page's controls. I doubt any controls you put directly ContentPlaceHolder within master page are even getting rendered?</p> <p>Do you see the other controls Price, and all while the page gets opened in browser?</p> 2012-05-06T09:45:24-04:004967546http://forums.asp.net/p/1800526/4967546.aspx/1?Re+can+t+find+labelRe: can't find label <h3 class="post-title"><a title="nirman.doshi" href="http://forums.asp.net/members/nirman.doshi.aspx">nirman.doshi</a>&nbsp;</h3> <p>no you are wrong look carefully to the code in the first snippet there is a directive call <strong>masterPagefile</strong>=&quot;&quot; which means that this is a child or content and it has the label name</p> <p>in the second snippet there is the master look at page directive the first thing is master.</p> <p>so he want to access the child content from the master page .</p> 2012-05-06T09:59:23-04:004967552http://forums.asp.net/p/1800526/4967552.aspx/1?Re+can+t+find+labelRe: can't find label <p>Hello,</p> <p>As far as I know, a simple code would do this</p> <pre class="prettyprint">Master.FindControl(&quot;<strong>lblName</strong>&quot;);</pre> <p>Are you sure the labels get rendered on the webpage? Please check the code behind and verify that you are not setting those to Visible=false.</p> 2012-05-06T10:12:00-04:004967553http://forums.asp.net/p/1800526/4967553.aspx/1?Re+can+t+find+labelRe: can't find label <p>Oh sorry.. I misunderstood completely.</p> 2012-05-06T10:12:31-04:004967556http://forums.asp.net/p/1800526/4967556.aspx/1?Re+can+t+find+labelRe: can't find label <p>I think, you should be able to access it using -</p> <p>this.MainContent.FindControl(&quot;lblInfo&quot;)</p> <p>in Page_Load, or PreRender of Master page.</p> 2012-05-06T10:15:24-04:004967665http://forums.asp.net/p/1800526/4967665.aspx/1?Re+can+t+find+labelRe: can't find label <p></p> <blockquote><span class="icon-blockquote"></span> <h4>muybn</h4> <p></p> <p>Thanks, but same error message. It's just not finding <strong>lblName</strong>. 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.</p> <p></p> </blockquote> <p></p> <p>Please double check the solution i posted. &nbsp;I took the exact master page you posted as well as your exact content page and validated that what i posted works correctly. &nbsp;So unless you're doing something different than what you'd originally posted, it should work. &nbsp;If you do have something else going on beyond what you'd posted, you'll need to share what it is.</p> 2012-05-06T13:24:28-04:004967708http://forums.asp.net/p/1800526/4967708.aspx/1?Re+can+t+find+labelRe: can't find label <p>I discovered that the label is being found after all, even without the FindControl feature. The error was coming from a session variable that wasn't initializing. The problem has changed so if anyone wants to change gears and help with the following description, I would appreciate it.</p> <p>The problem originates in the fact that when I click the lnkButton control and the lnk_Click event isn't accessed by the program. The flow in the code below goes from Products.aspx to Checkout.aspx. Any suggestions?</p> <pre class="prettyprint">&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.master&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Products.aspx.cs&quot; Inherits=&quot;About&quot; Title=&quot;Select a Product&quot; %&gt; &lt;asp:Content ID=&quot;HeaderContent&quot; runat=&quot;server&quot; ContentPlaceHolderID=&quot;HeadContent&quot;&gt; &lt;/asp:Content&gt; &lt;asp:Content ID=&quot;BodyContent&quot; runat=&quot;server&quot; ContentPlaceHolderID=&quot;MainContent&quot;&gt; &lt;table width=&quot;900&quot;&gt; &lt;tr align=&quot;center&quot;&gt; &lt;td colspan=&quot;5&quot; align=&quot;center&quot;&gt; &lt;asp:Image ID=&quot;imgLock&quot; ImageUrl=&quot;~/i/wwb_img6.jpg&quot; runat=&quot;server&quot; /&gt; &lt;h2&gt; Select a Product: &lt;/h2&gt; &lt;br /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr align=&quot;center&quot;&gt; &lt;td align=&quot;center&quot;&gt; &lt;asp:ImageButton ID=&quot;lnk1&quot; AlternateText=&quot;Blackberry&quot; ImageUrl=&quot;~/i/Blackberry_s.jpg&quot; PostBackUrl=&quot;~/Checkout.aspx&quot; runat=&quot;server&quot; onclick=&quot;<b><i>lnk_Click</i></b>&quot; /&gt;&lt;br /&gt; &lt;asp:Label ID=&quot;Label1&quot; Text=&quot;Blackberry&quot; runat=&quot;server&quot; /&gt; &lt;/td&gt; &lt;td align=&quot;center&quot;&gt; &lt;asp:ImageButton ID=&quot;lnk2&quot; AlternateText=&quot;HTC&quot; ImageUrl=&quot;~/i/HTC_s.jpg&quot; PostBackUrl=&quot;~/Checkout.aspx&quot; runat=&quot;server&quot; onclick=&quot;<b><i>lnk_Click</i></b><em>&quot; /&gt;&lt;br /&gt; &lt;asp:Label ID=&quot;Label2&quot; Text=&quot;HTC&quot; runat=&quot;server&quot; /&gt; &lt;/td&gt; &lt;td align=&quot;center&quot;&gt; &lt;asp:ImageButton ID=&quot;lnk3&quot; AlternateText=&quot;LG&quot; ImageUrl=&quot;~/i/LG_s.jpg&quot; PostBackUrl=&quot;~/Checkout.aspx&quot; runat=&quot;server&quot; onclick=&quot;</em><b><i>lnk_Click</i></b><em>&quot; /&gt;&lt;br /&gt; &lt;asp:Label ID=&quot;Label3&quot; Text=&quot;LG&quot; runat=&quot;server&quot; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/asp:Content&gt; //Products.aspx.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; public partial class About : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } </em><b><i>protected void lnk_Click(object sender, ImageClickEventArgs e)</i></b> //doesn't run { ImageButton lnk = ((ImageButton)sender); if (lnk != null) { string text1 = lnk.AlternateText; <b><i>Session[&quot;text&quot;] = text1;</i></b> } } } &lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.master&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Checkout.aspx.cs&quot; Inherits=&quot;Checkout&quot; %&gt; &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;HeadContent&quot; runat=&quot;Server&quot;&gt; &lt;/asp:Content&gt; &lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;Server&quot;&gt; &lt;asp:Panel id=&quot;div&quot; style=&quot;width: 900px; text-align: center;&quot; runat=&quot;server&quot;&gt; &lt;asp:Label ID=&quot;lblCheckout&quot; Font-Size=&quot;Large&quot; Text=&quot;Thank you for shopping. Order details:&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt; <b><i>&lt;asp:Label ID=&quot;lblName&quot; Font-Size=&quot;Large&quot; runat=&quot;server&quot; /&gt;</i></b> &lt;asp:Label ID=&quot;lblPrice&quot; Font-Size=&quot;Large&quot; Text=&quot;$100&quot; runat=&quot;server&quot; /&gt; &lt;/asp:Panel&gt; &lt;/asp:Content&gt; //Checkout.aspx.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Checkout : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Label lbl = Util.FindChild(Page, &quot;lblName&quot;) as Label; <b><i>//label is being found OK</i></b> if (lbl != null) { <b><i>//error here because session variable didn't initialize in code above</i></b> <b><i>//lbl.Text = Session[&quot;text&quot;].ToString();</i></b> } } }</pre> <p><br> <br> </p> 2012-05-06T14:35:19-04:004967755http://forums.asp.net/p/1800526/4967755.aspx/1?Re+can+t+find+labelRe: can't find label <p></p> <blockquote><span class="icon-blockquote"></span> <h4>muybn</h4> <p></p> <pre class="prettyprint">PostBackUrl=&quot;~/Checkout.aspx&quot;</pre> <p></blockquote></p> <p>Hi</p> <p>Remove&nbsp;<em>PostBackUrl</em> property from ImageButtons and insert it inside your method as below</p> <pre class="prettyprint"> protected void lnk_Click(object sender, ImageClickEventArgs e) //doesn't run { ImageButton lnk = ((ImageButton)sender); if (lnk != null) { string text1 = lnk.AlternateText; Session["text"] = text1; } Response.Redirect("~/Checkout.aspx"); }</pre></blockquote> 2012-05-06T15:55:13-04:004967781http://forums.asp.net/p/1800526/4967781.aspx/1?Re+can+t+find+labelRe: can't find label <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Ruchira</h4> <p></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>muybn</h4> <p></p> <pre class="prettyprint">PostBackUrl=&quot;~/Checkout.aspx&quot;</pre> <p></p> </blockquote> <p></p> <p>Hi</p> <p>Remove&nbsp;<em>PostBackUrl</em> property from ImageButtons and insert it inside your method as below</p> <pre class="prettyprint"> protected void lnk_Click(object sender, ImageClickEventArgs e) //doesn't run { ImageButton lnk = ((ImageButton)sender); if (lnk != null) { string text1 = lnk.AlternateText; Session["text"] = text1; } Response.Redirect("~/Checkout.aspx"); }</pre> <p></p> </blockquote> I'm not sure why, but without any changes that I'm aware of, the lnk_Click event suddenly started being recognized (in other words, the breakpoint finally made the execution pause). I found that both the PostBackURL and the Response.Redirect commands worked equally well but I think I will use Response.Redirect along with a QueryString with it instead of the session variable. If the information to be passed were more secure, I would use a session variable but QueryString in this case, used without any sensitive information, will cut down on bandwidth. Thanks again to everyone for all your help. <p></p> </blockquote> 2012-05-06T16:52:52-04:004967791http://forums.asp.net/p/1800526/4967791.aspx/1?Re+can+t+find+labelRe: can't find label <p>You cannot have both, event and a postbackurl. When you specify a postbackulr, it will redirect to the specified page without firing the event. You can have a client click event. It will get fired because it will run BEFORE the server side code runs.</p> 2012-05-06T17:07:06-04:00