简体   繁体   English

如何从另一个ascx.cs文件调用ascx.cs文件中的函数?

[英]How to call the function in ascx.cs file from another ascx.cs file?

I have two different ascx controls. 我有两个不同的ascx控件。 They placed in the same folder. 他们放置在同一文件夹中。 How can I call function in one file from another file if there are no namespaces specified? 如果未指定名称空间,如何在一个文件中调用另一个文件中的函数?

Example of the file which contains a function I need to call. 包含我需要调用的函数的文件示例。


    public partial class atilektcms_cmsmoduls_security_security : StoreAwareUserControl
    {
        private void LoadUserEdit()
        {
             //function's actions
        }

    }

It is easy to see that here is no namespace declared. 不难看出这里没有声明名称空间。 Can I declare a namespace for this file? 我可以为此文件声明名称空间吗? How to do this correctly? 如何正确做到这一点?

What also I need to do to make this function callable from another ascx.cs file? 我还需要做些什么才能使此函数可从另一个ascx.cs文件调用? First of all I need to change the "private" to "public" I think. 首先,我需要将我认为的“私有”更改为“公开”。 What's else? 有什么别的?

So what you want is a custom event on your 'child' control: 因此,您想要的是“子级”控件上的自定义事件:

public event EventHandler SomethingHappened;

Which you are then going to raise (if handled) in your button click event within your child control: 然后,您将在子控件中的按钮单击事件中引发(如果得到处理):

protected void myButton_Click(object sender, EventArgs e)
{
    if (SomethingHappened!= null)
    {
        this.SomethingHappened(this, EventArgs.Empty);
    }
}

And you then need to handle this event within your parent control or page and react appropriately. 然后,您需要在父控件或页面中处理此事件并做出适当反应。

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

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