将值从child.master传递给parent.master

时间:2011-06-28 21:40:36

标签: c# asp.net

我需要将一个字符串值从child.master传递给parent.master。 parent.master中有一个函数

public void setMyString(string s)
{
myLiteral.Text = s;
}

如何从我的child.master访问此类方法?

3 个答案:

答案 0 :(得分:2)

你必须将Child.master的Master转换为Parent.master的类才能调用该类的方法。

假设Parent.master的类名是“ParentMasterPage”。在Child.master中,您可以拨打这样的电话:

((ParentMasterPage)this.Master).setMyString(param);

答案 1 :(得分:1)

如果您只想调用类的父函数,请不要在子类中覆盖它,或者在子函数中调用基本方法。

如果要调用该父类的特定实例的父函数,则需要在子类实例中引用该实例。

答案 2 :(得分:0)

这是一个很好的做法,根本不依赖于某些Parent组件,只是想象您将来决定在子页面和父母主页之间粘贴一些其他母版页 - 并且由于引用您将遇到麻烦到前父母主页。

所以,在我看来,最好在childmaster页面上定义一个事件,并在父母版主页面上为这个事件设置一个事件处理程序。