简体   繁体   中英

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

I have two different ascx controls. 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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