0

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")
4

2 回答 2

1

您不能通过直接从代码中操作页面标记来添加服务器端控件,因为此时页面已经创建。如果要动态创建控件,然后从服务器端代码访问它们,则需要将它们添加到Controls集合中。

您必须通过实际的服务器端代码构建您的 ASP.NetHtmlTable服务器端控件(包括HtmlTableRowHtmlTableCell和所有相关控件),并将生成的表添加到 userCP 中,例如userCP.Controls.Add(myCreatedTable)

于 2013-06-05T18:07:33.593 回答
0

只是为了清理、澄清和集中答案和评论:

不管我使用 LiteralControl 还是 Asp:Table,我需要的“displayName”现在作为全局变量存储在 default.master.cs 文件中:

string displayName = "Guest";

public string dName()
{
    return displayName;
}

并且可以使用以下命令从任何内容页面中查找:

//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;
}

但是,如果我确实需要从表中的单元格中提取信息而不将其作为变量存储在 default.master.cs 页面上,我也可以使用 ASP 表而不是 LiteralControl 然后查找标签的id:

主代码文件:

Label l;

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

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

内容代码文件:

string dName = ((_default)Master).FOO();
于 2013-06-05T19:43:02.093 回答