2013-06-05 30 views
0

我有一個主頁面「default.master」和一個內容頁面「Store.aspx」,每個頁面都有自己的代碼隱藏頁面。在母版頁的代碼隱藏中,我動態地創建一個包含一些文本的標籤。我想在'Store.aspx.cs'的代碼隱藏頁面中查找標籤的文本,但我的代碼返回'null'控件...從動態創建控件的主頁訪問「HtmlGenericControl」

在我的default.master.cs頁面負載:

<label runat=\"server\" id=\"displayname\"><a href='MyAvatar.aspx'>" + displayName + "</a></label> 

在我的頁面加載Store.aspx.cs:

HtmlGenericControl dName = (HtmlGenericControl)Master.FindControl("displayName"); 
if (dName != null) 
    Debug.Print("dName: " + dName.InnerText); 

這不工作...但是,如果我直接把標籤在default.master頁面這樣:

<label runat="server" id="displayName">TEST</label> 

然後當我加載Store.aspx,我清楚地看到我的調試輸出:

dName: TEST 

我真正需要的標籤動態創建,也需要能夠訪問標籤的內容不僅來自Store.aspx.cs,但也來自其他兩個頁面(以及未來可能更多)。有任何想法嗎??

編輯: 我只是想指出,動態創建的標籤是一個較大元素的一部分...標籤是一個表格單元格內,錶行內,一張桌子,所有這些都在裏面文字內控制......爲什麼不在這裏粘貼整個事情呢? (標籤是在第4行)

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>"; 

編輯: 根據所給出的第一個答案,而不是字面控制爲「userCP」,我轉身到這一個ASP表並添加在代碼行/細胞-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); 

我store.aspx.cs仍返回null:

Debug.Print("Name: " + Master.FindControl("displayName")); 

即使我投查找:

(HtmlGenericControl)Master.FindControl("displayName") 
(Label)Master.FindControl("displayName") 

回答

1

您不能通過從代碼直接操作頁面標記來添加服務器端控件,因爲頁面已經在那個時候創建​​。如果您想動態創建控件,然後從服務器端代碼訪問它們,則需要將它們添加到Controls集合中。

你必須建立你的ASP.Net HtmlTable服務器端控件(包括HtmlTableRow和表HtmlTableCell和所有相關的控制)通實際的服務器端代碼和生成的表的東西添加到userCP像userCP.Controls.Add(myCreatedTable)

+0

明白了!我在想,情況會是這樣,謝謝你的確認! –

+0

所以我仍然有一個問題...在default.master.cs中,我有: 'l.ID =「displayName」; // l = new Label()' 並在Store.aspx.cs中: 'HtmlGenericControl dName =(HtmlGenericControl)Master.FindControl(「displayName」); (dName!= null) Debug.Print(「dName:」+ dName.InnerText);' 仍然返回空... –

+0

在呈現的ASPX頁面中,執行查看HTML源代碼並查看「displayName」id已成爲 –

0

只是爲了清理,澄清,並集中解答和意見:

如果我使用LiteralControl或天冬氨酸沒關係:表中,「顯示名」我需要的是現在存儲作爲一個全局變量default.master。CS文件:

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

但是,如果我真的需要拉從表中的單元格中的信息,而不把它作爲一個:

string displayName = "Guest"; 

public string dName() 
{ 
    return displayName; 
} 

,可以從任意內容網頁使用進行查找在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();