获取母版页 控件
获取母版页的相关内容有两种方法
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();
如何在后台代码中设置和获取用户控件中的子控件的属性?
add public properties/methods in your codebehind class for your usercontrol and declare in your page class:
protected YourUserControlCodeBehindClass YourUserControlID;
..
if you don't have a codebehind class, you can always do
UserControl c = (UserControl)FindControl("YourUserControlID");
TextBox tb = (TextBox) c.FindControl("YourTextBoxIDInYourUserControl);
tb.Text = "123";
>>>2、如何在后台设置和获取用户控件本身的属性?
if you have a codebehind class for your usercontrol, you can do
protected YourUserControlCodeBehindClass YourUserControlID;
YourUserControlID.Property1 = "123";
otherwise, you have to use Reflection
获取模板页里用户控件里的控件
control_left2 c = (control_left2)Master.FindControl("Left2_1");
((Label)c.FindControl("Label1")).Text = model.CompanyPoint.ToString();
简单整理版。