Question

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")
Was it helpful?

Solution

You cannot add server-side control by directly manipulating page markup from code, because page is already created by that time. If you want to create controls dynamically and then access them from server-side code you need to add them to Controls collection.

You have to build your ASP.Net HtmlTable server-side control (including HtmlTableRow and table HtmlTableCell and all the related controls) thru actual server-side code and add the resulting table to the userCP with something like userCP.Controls.Add(myCreatedTable)

OTHER TIPS

Just to clean up, clarify, and centralize the answers and comments:

It doesn't matter if I use the LiteralControl or the Asp:Table, the 'displayName' I need is now stored as a global variable in the default.master.cs file:

string displayName = "Guest";

public string dName()
{
    return displayName;
}

And can be looked up from any content page using:

//Make sure the user has an account
string dName = ((_default)Master).dName();
if (dName == "Guest")
{
    LiteralStoreHeader.Text = "<center><b>We're Sorry!</b><br/>You must have an Avatar account to view this page.</center>";
    return;
}

However, if I really do need to pull the info from a cell in the table without storing it as a variable on the default.master.cs page, I can also do that by using the ASP Table instead of the LiteralControl and then looking up the id of the Label:

Master Code File:

Label l;

public string FOO()
{
    return l.Text;
}

protected void Page_Load(object sender, EventArgs e)
{
    l = new Label();
    l.Text = "Bar";
}

Content Code File:

string dName = ((_default)Master).FOO();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top