1 通過findcontrol找控件ID
需要在此事件中~因為Page_load中時是先內容頁加載然后才是母版頁加載
protected void Page_LoadComplete(object sender, EventArgs e)
{
Label2.Text = "現在時間是" + (Master.FindControl("Label1") as Label).Text;
if (Request.QueryString["id"] == "dy")
{
(Master.FindControl("Image1") as Image).ImageUrl = "~/Images/ml0069.jpg";
}
}
2 通過強引用
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>
然后可以在母版頁中定義公共屬性或方法
public string GetUserName()
{
return Page.User.Identity.Name;
}
在內容頁中調用
Label1.Text = "歡迎光臨" + Master.GetUserName();
================================================
1、如何在后臺代碼中設置和獲取用戶控件中的子控件的屬性?
UserControl c = (UserControl)FindControl("YourUserControlID");
TextBox tb = (TextBox) c.FindControl("YourTextBoxIDInYourUserControl);
tb.Text = "123";
2、如何在后臺設置和獲取用戶控件本身的屬性?
YourUserControlID.Property1 = "123";
獲取模板頁里用戶控件里的控件
control_left2 c = (control_left2)Master.FindControl("Left2_1");
((Label)c.FindControl("Label1")).Text = model.CompanyPoint.ToString();