简体   繁体   English

从 UserControl 到父母版页

[英]From UserControl to parent master page

I have a weird scenario but this was how it was designed before me.我有一个奇怪的场景,但这就是它在我之前的设计方式。 Basically I have userControl, and there is a child.masterpage基本上我有userControl,还有一个child.masterpage

in the userControl in the ascx file it contains the following在 ascx 文件的 userControl 中,它包含以下内容

<div><%=_template%></div>

the child.masterpage inherits from a parent.masterpage, in the child.masterpage there is a call to the userControl child.masterpage 继承自 parent.masterpage,在 child.masterpage 中有对 userControl 的调用

<asp:Content><ucc:UserControl></ucc>

the parent.masterpage has other fields in it and it has a.cs file with a c# function parent.masterpage 中有其他字段,它有一个带有 c# function 的.cs文件

public void passVal(string s)

Now what I want to do is to pass a value from the user control directly to the parent.masterpage function so that I can put it in the parent.masterpage literal I have created.现在我要做的是将用户控件中的值直接传递给 parent.masterpage function 以便我可以将它放在我创建的 parent.masterpage 文字中。

How can I achieve this (again, this is existing design and I cant turn things around) I am just adding a functionality.我怎样才能实现这一点(同样,这是现有的设计,我无法扭转局面)我只是在添加一个功能。

<%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/main.master" %>
<%@ Register Src="UserControl.ascx" TagName="Ord" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="in"><uc:OrderReceipt ID="myord" runat="server" Visible="true"/>
 <div style="margin-bottom:30px;">
<a href="~/" id="HomeLink" runat="server" class="BackLink">Back to Home Page</a>
</div>
</asp:Content>

You can use the Page.Master property to get a hold of the master page instance.您可以使用Page.Master属性来获取母版页实例。

protected someEvent(object sender, EventArgs e)
{
   (Page.Master as ChildMaster).passVal("some string");
}

More of an Answer:更多答案:

I was just reviewing your OP and realized something.我只是在查看您的 OP 并意识到一些事情。 The code that kept puzzling me was the user control.一直困扰我的代码是用户控件。

in the userControl in the ascx file it contains the following在 ascx 文件的 userControl 中,它包含以下内容

<div><%=_template%></div>

I haven't seen the code behind but my guess is that the user control is simply used to output dynamic HTML.我还没有看到后面的代码,但我的猜测是用户控件只是用于 output 动态 HTML。 I bet if you looked at the code behind (.cs file) of the user control, you would find a variable called _template .我敢打赌,如果您查看用户控件背后的代码(.cs 文件),您会发现一个名为_template的变量。 It is a string variable that is pumped with html at run time.它是在运行时使用 html 抽取的字符串变量。

Now, that doesn't answer your question but, if you didn't already know that... it is good to know =P现在,这并不能回答你的问题,但是,如果你还不知道......很高兴知道 =P

Now, the next mystery is the one concerning your missing code behind file for the child master page.现在,下一个谜团是关于您丢失的子母版页文件隐藏代码的谜团。

My theory is that whoever made it did it with some error that would cause it not to automatically generate a code behind file.我的理论是,无论是谁做了它,都会出现一些错误,导致它不会自动生成隐藏文件的代码。 Or, they made it from scratch and just simply added it to the project but neglected to make a code behind as well.或者,他们从头开始制作,只是简单地将其添加到项目中,但也忽略了后面的代码。

I made a master page, then made another one called child.我制作了一个母版页,然后制作了另一个名为 child 的母版页。 I am able to subclass it and here is what the markup and code behind look like.我可以对它进行子类化,这就是背后的标记和代码的样子。

<%@ Master Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true"   CodeFile="Child.master.cs" Inherits="Child" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
</asp:Content>

public partial class Child : Master
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

In comparing the markup to yours, the key difference here is that mine explicitly mentions a CodeFile attribute.在将标记与您的标记进行比较时,这里的主要区别在于我明确提到了CodeFile属性。

Create a new cs file and following the naming convention.创建一个新的 cs 文件并遵循命名约定。 Then add the CodeFile and Inherits attributes to your child master page.然后将CodeFileInherits属性添加到您的子母版页。 This should wire everything up correctly and allow you to start adding methods and such to the child master page.这应该正确连接所有内容,并允许您开始向子母版页添加方法等。

Let me know where you are at and we'll take it from there.让我知道你在哪里,我们会从那里拿走它。 GL总帐

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM