在asp.net中,如何訪問非主頁面中的主頁面控件?在非母版頁中訪問主頁面控件?
3
A
回答
5
您可以訪問母版頁作爲您當前頁面上的一個屬性。但是,母版頁上的控件受到保護,因此您無法直接訪問它們。但是您可以使用FindControl(string name)
訪問它們。您需要使用的代碼取決於控件位於內容佔位符之內還是之外。
// Gets a reference to a TextBox control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
if(mpTextBox != null)
{
mpTextBox.Text = "TextBox found!";
}
}
// Gets a reference to a Label control that is not in a
// ContentPlaceHolder control
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
Label1.Text = "Master page label = " + mpLabel.Text;
}
+0
誰是'Label1'? – 2016-04-06 12:47:17
2
在網頁中添加此訪問母版頁的內容Master Page : programatically access
<%@ MasterType virtualpath="Your MasterPath" %>
你可以做這樣的(另一種方式)
MasterPage mstr
Label lbl
mstr = Page.Master
If (mstr.ID == "yourMasterIDString")
{
lbl = mstr.FindControl("lblBar")
If (lbl !=null)
{
lbl.Text = "Do some Logic"
}
}
2
的使用可以
TextBox txt1 = (TextBox)this.Master.FindControl("MytxtBox");
txt1.Text="Content Changed from content page";
相關問題
- 1. 母版頁控件在子頁面中?
- 2. 在母版頁中使用javascript在母版頁中訪問控件
- 3. 從我的班級訪問母版頁/子頁面控件
- 4. 如何訪問控件的主要母版頁從內容頁面嵌套母版頁
- 5. 頁面上與母版頁從用戶控制訪問控制
- 6. 在其他頁面訪問/修改母版頁的控制 - Asp.net
- 7. 從子頁面訪問母版頁面的TR頁面
- 8. 訪問母版頁在C#
- 9. 在母版頁的內容頁面中控制用戶控件
- 10. 從母版頁中的用戶控件訪問頁面公共屬性
- 11. 如何從內容頁面訪問母版頁中的用戶控件?
- 12. 從ascx控制訪問母版頁
- 13. 從WebMethod訪問主頁面控件
- 14. 使用javascript從內容頁訪問母版頁控件
- 15. 如何在用戶控件中訪問主頁面控件值
- 16. 在ascx文件中訪問母版頁控制
- 17. 訪問來自usercontrol的主控頁面控制的封裝在母版頁中的aspx
- 18. 從父母母版頁的子母版頁訪問元素
- 19. 訪問母版頁屬性
- 20. 我們可以從母版頁訪問內容頁面控件嗎?
- 21. 如何在孩子的.aspx頁面訪問母版頁的控制(在JavaScript)
- 22. asp.net - 從子頁面訪問母版頁中的公共類
- 23. 無法訪問內容頁面中的嵌套母版頁的控制
- 24. 內容頁面是否可以使用其母版頁面的母版父母的主ContentPoltageHolderID(嵌套母版頁)
- 25. 在Web用戶控件上訪問主頁面控件
- 26. 訪問母版頁上的Web控件中的文本框
- 27. 如何訪問我的孩子頁面.aspx頁面中我的母版頁值
- 28. 隱藏母版頁控件
- 29. asp.net中的打印頁面沒有母版頁的控件
- 30. 無法在頁面母版頁上找到控件
是您正在渲染使用的視圖g擁有控件的主頁面? – Brian 2012-01-30 18:57:23