如何从母版调用一个函数到在C#中的新形式 [英] How to call a function from Masterpage into new form in c#

查看:136
本文介绍了如何从母版调用一个函数到在C#中的新形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版页,其中包含一个的DropDownList 。我有一个功能,在主绑定列表,它工作正常。

I've a Master Page which contains a DropDownList. I've a function for binding the list in the master and it works fine.

我的问题是:我如何调用,从形式,这是不是上述母版页的子母版页功能

My problem is: How will I call that Master Page function from a form, which is not the child of the above master page

推荐答案

请参阅的文章这里。

这是一件好事来自ASP.NET 2.0中的新编译模型。比方说,你的自定义属性添加到母版页code-后面像这样的文件:

Here is something nice that comes from the new compilation model in ASP.NET 2.0. Let’s say you add a custom property to a master page code-behind file like so:

partial class otcMaster : System.Web.UI.MasterPage
{

public string FooterText {
    get { return Footer.Text; }
    set { Footer.Text = value; }
}

 }

您可以用继承的Master属性,它返回一个母版的参考机会到Web表单的母版页。为了得到在otcMasterPage定义虽然财产,你可能会认为你需要使用一个转换。

You can get to the master page for a web form using the inherited Master property, which returns a MasterPage reference. To get to a property defined in otcMasterPage though, you might think you need to use a cast.

((otcMaster)Master).FooterText == "foo"

转换为派生类型是生命的一部分,使用框架和静态类型语言的时候,但有一个更好的办法。使用在ASPX的@指令的MasterType

Casting to a derived type is just a part of life when using frameworks and statically typed languages, but there is a better way. Use the @ MasterType directive in the ASPX.

<%@ MasterType VirtualPath="~/otc.master"  %>

现在,当ASP.NET codegens的页面,它把一个局部类定义如下。注意阴影关键字(这将是在土地分号[是啊,我用其他语言实验]新的关键字)。

Now when ASP.NET codegens the page, it puts the following inside a partial class definition. Notice the Shadows keyword (that would be the new keyword in semicolon land [yeah, I’m experimenting with alternative languages]).

public new otc Master {
get { return (otcMaster)base.Master; }
}

的结果是一个强类型主页。我们不需要打石膏,我们可以去正确的Master.FooterText财产。另一种方式做,这是指定的@MasterType指令类型名。

The result is a strongly typed Master Page. We don’t need a cast, we can go right to the Master.FooterText property. Another way to do this is to specify a TypeName in the @MasterType directive.

这篇关于如何从母版调用一个函数到在C#中的新形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆