ASP.NET中调用一个方法(CSS)从用户控件(ASCX)? [英] ASP.NET Calling a Method in (ASPX) From a UserControl (ASCX)?
问题描述
有一个 Default.aspx的
其codebehind有一个方法:公共无效DoSomething的(){}
its Codebehind has a Method: public void DoSomething(){}
的 Default.aspx的
得到了一个 UserControl.ascx
在的codebehind我的 UserControl.ascx
我想打电话给我的 DoSomething的()
从 Default.aspx的
,但这并不作品:
In the Codebehind of my UserControl.ascx
I would like to call my DoSomething()
from Default.aspx
, but this doesn't works:
Default defaultPage = new Default();
defaultPage.DoSomething();
我怎样才能做到这一点?
( Default.aspx的
也是母版的StartupPage)
How can I achieve this?
(Default.aspx
is also the StartupPage of the Masterpage)
推荐答案
默认defaultPage =新的默认();
将创造你的页面的新实例,其中ISN t您想要的东西。
Default defaultPage = new Default();
would create a new instance of your page, which isn't what you want.
从你的用户控件,你可以做这样的事情:
From your usercontrol, you could do something like this:
((Default)Page).DoSomething();
或者是安全的,并确保在页面类型默认,因为用户控件可以在许多不同的页面存在(这就是为什么这未必是最好的主意)。
Or to be safe and ensure that the Page is of type Default since a user control could exist on many different pages (which is why this may not be the best idea).
Default p = Page as Default;
if( p != null )
p.DoSomething();
这篇关于ASP.NET中调用一个方法(CSS)从用户控件(ASCX)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!