[英]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.