简体   繁体   English

我们可以从母版页访问内容页面控件吗?

[英]Can we access content page control from master page?

I have a master page which is used for several content pages.我有一个用于多个内容页面的母版页。 On every content page there is a user control.在每个内容页面上都有一个用户控件。 I want to call a method of that control on click of a button which is in the master page.我想在单击母版页中的按钮时调用该控件的方法。

Is it possible?是否有可能?

You can do this to access content page control您可以执行此操作来访问内容页面控件

protected void Button1_Click(object sender, EventArgs e)
{
    UserControl_Type uc1 = 
    (UserControl_Type)ContentPlaceHolder1.FindControl("UserControlId");
    if (uc1 != null)
    {
        uc1.Method();
    }
}

I think you should move UserControl to master page and access usercontrol method in button click in masterpage cs file.我认为您应该将 UserControl 移至母版页,并在母版页 cs 文件中的按钮单击中访问 usercontrol 方法。 You can do following if there are are any content pages you dont want to show this user control如果有任何您不想显示此用户控件的内容页面,您可以执行以下操作

Using content place holders:使用内容占位符:

In Master Page: Add:在母版页中: 添加:

<asp:contentplaceholder id="UcControlPlaceHolder" runat="server">
<!-- User control here -->
</asp:contentplaceholder>

In Content Pages where you don't want to show UserControl add following tag and remove this tag from all content pages where you want UserControl to show:在你希望显示用户控件添加以下标记并删除您希望用户控件显示所有内容页这个标签内容页:

<asp:Content ID="UcControlContent1" 
ContentPlaceHolderID="UcControlPlaceHolder" Runat="Server">    
</asp:Content>

I want to call method of that control on click of button which is on master page.我想在单击母版页上的按钮时调用该控件的方法。 Can it be possible???可以吗???

Yes and no.是和否。 If you're talking client-side then yes, you could invoke a method on a control via JavaScript.如果您在谈论客户端,那么是的,您可以通过 JavaScript 调用控件上的方法。 If you're talking server-side then no.如果您说的是服务器端,则不。


However, I do have to ask, if the control is available on every content page, it's a very good candidate for existing on the master page yes?但是,我确实要问,如果控件在每个内容页面上可用,那么它是母版页上存在的一个很好的候选,是吗? You can hide it if you don't need it in specific content pages.如果您在特定内容页面中不需要它,您可以隐藏它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从内容页面访问母版页的<input type =“button”> - Can not access <input type=“button”> of a master page from a content page 内容页面上的母版页控件 - master page control on content page 无法从用户控件访问母版页 - Can't access master page from user control 我们可以通过登录按钮从登录页面(非内容页面)重定向到母版页吗? - Can we redirect from login page(non content page) to master page via login button? 如何访问内容页面中母版页的动态加载的服务器控件? - How to access Dynamically loaded server control of Master Page in content page? 当我的控件和数据来自母版页时,如何将数据传递到服务器并将页面重定向到下一个内容页? - How can pass data to server and page redirected to next content page as my control and data is from master page? 从内容页面访问母版页控件(html:输入)不起作用 - Access Master Page control (html: Input) from Content Page doesn't work 使用asp.net从母版页到内容页的访问下拉控件 - access dropdown control from the master page to the content page using asp.net 我们可以使用HttpHandler和HttpModule从母版页更改内容页div吗? - Can we use HttpHandler and HttpModule to change the content page div from the Master page? 从内容页面调用母版页中的用户控制方法 - Call User Control Method in Master Page from Content Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM