I have a master page "default.master" and a content page "Store.aspx", each with their own code-behind page. In the code-behind of the master page, I am dynamically creating a label with some text in it. I want to look up the text of the label in the code-behind page for the 'Store.aspx.cs' but my code is returning 'null' controls...
In my default.master.cs on page load:
<label runat=\"server\" id=\"displayname\"><a href='MyAvatar.aspx'>" + displayName + "</a></label>
In my Store.aspx.cs on page load:
HtmlGenericControl dName = (HtmlGenericControl)Master.FindControl("displayName");
if (dName != null)
Debug.Print("dName: " + dName.InnerText);
This does NOT work... However, if I put a label directly in the default.master page like this:
<label runat="server" id="displayName">TEST</label>
Then when i load the Store.aspx, I clearly see in my Debug Output:
dName: TEST
I really need the label to be created dynamically and also need to be able to access the content of the label not only from the Store.aspx.cs but from 2 other pages as well (and potentially more in the future). Any ideas??
EDIT: I just wanted to note that the dynamically created label is part of a larger element... The label is inside a table cell, inside a table row, inside a table, all of which is inside a literal control... What the heck, why not paste the whole thing here huh? (Label is in the 4th line)
userCP.Text = "<table width=100% border=0 cellpadding=0 cellspacing=0>" +
"<tr><td colspan=4 style=\"font-size: large\"><center><b>Welcome,</b></center></td></tr>" +
"<tr style=\"height: 5px; \"><td colspan=4></td></tr>" +
"<tr><td colspan=4><center><label runat=\"server\" id=\"displayname\"><a href='MyAvatar.aspx'>" + displayName + "</a></label></center></td></tr>" +
"<tr style=\"height: 5px; \"><td colspan=4></td></tr>" +
"<tr><td colspan=4><hr/></td></tr>" +
"<tr><td>" + leaderLink + "</td><td width=100% id=\"userCP_bones\" style=\"text-align:right; \">" + bones + "</td><td style=\"text-align:left;\"><b>/</b><a href='Store.aspx?SC=Acct' title='Click to upgrade your wallet!'>" + maxBones + "</a></td><td><center><img src='images/bone.png' alt='Bones' align='right' style=\"vertical-align:middle\"/></center></td></tr>" +
"<tr><td></td><td></td><td id='CartCount' style=\"text-align:right; text-decoration: underline; cursor:pointer; \"><a href='Checkout.aspx'>" + itemCount + "</a></td><td width=10%><center><a href='Checkout.aspx'><img src='images/cart.png' onmouseover=\"this.src='images/cart_h.png'\" onmouseout=\"this.src='images/cart.png'\" height='24' width='24' alt='Cart' align='right' style=\"vertical-align:middle; border:none\"/></a></center></td></tr>" +
"</table>";
EDIT: Based on the first answer given, instead of a literal control for "userCP", I turned that into an ASP table and add rows/cells in the code-behind:
//Row for display name
TableRow r = new TableRow();
userCP.Rows.Add(r);
TableCell c = new TableCell();
r.Cells.Add(c);
c.ColumnSpan = 4;
Label l = new Label();
l.Text = "<center><a href='MyAvatar.aspx'>" + displayName + "</a></center>";
l.ID = "displayName";
c.Controls.Add(l);
My store.aspx.cs is still returning null when doing:
Debug.Print("Name: " + Master.FindControl("displayName"));
Even if I cast the lookup:
(HtmlGenericControl)Master.FindControl("displayName")
(Label)Master.FindControl("displayName")