代码之家  ›  专栏  ›  技术社区  ›  Bryan Denny

如何从内容页访问母版页中的用户控件?

  •  5
  • Bryan Denny  · 技术社区  · 16 年前

    假设母版页中有一个页眉用户控件,并且希望根据母版页中加载的内容页更改用户控件的属性。我该怎么办?

    谢谢

    4 回复  |  直到 16 年前
        1
  •  13
  •   BornToCode    8 年前

    您可以使用两种方法。第一种是使用 Page.Master.FindControl('controlID') . 然后可以将其强制转换为用户控件的类型。第二种方法是添加 <%@ MasterType VirtualPath=""> <%@ MasterType TypeName=""%> 标记到您的aspx页面。在 VirtualPath 将虚拟路径添加到母版页或中的类 TypeName

        2
  •  5
  •   Josh Lee ZZ Coder    13 年前

    首先在母版页中找到用户控件,如下所示。然后找到访问其属性所需的控件。

    UserControl uch = Page.Master.FindControl("ucHeader1") as UserControl;
    PlaceHolder phProxylist= ucHeader1.FindControl("phProxy") as PlaceHolder;
    DropDownList ddlproxylist1 = ucHeader1.FindControl("ddlProxyList") as DropDownList;
    phProxylist.Visible = false;
    

    希望这有帮助。

        3
  •  4
  •   Robert C. Barth    16 年前

        4
  •  1
  •   Mark Carpenter    16 年前

    使用公共财产是可行的。在内容页的FormLoad方法中,可以执行以下操作(VB):

    Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
    myMaster.MyUserControl.Text = "Hello!"