How do I call a method that is in a web page class file from a user control in SharePoint 2007?
I followed the instructions in this MSDN article: http://msdn.microsoft.com/en-us/library/dd206945.aspx
Is it possible to call the method that is in the myWebPart.cs file from the MyUserControl.ascx file? I don't seem to have intellisense methods in myWebPart.cs, if I don't:
myWebpart mywbprt = new myWebpart();
mywbprt.myMethInWebPartcs();
However, this gets an error and doesn't compile:
Error 2 'myWebpart' is a 'namespace' but is used like a 'type'
MyWebPart.cs and MyUserControl.ascx are using the same namespace and I thought this would be enough to call methods in myWebPart.cs in usercontrol, but apparently not?
Is SharePoint lacking in complexity?
a source to share
First of all, it looks like the namespace myWebPart is in has the same name as the web part. You have to change that. You probably won't have a compile time error.
You will have a runtime error. If this method that you want to call interacts with the state of the web part runtime, it shouldn't be called from outside the web part, most likely.
Moreover, the custom control needs to find and invoke a specific instance of the currently running Web Part, not create a new instance that has nothing to do with the instance that is already running.
If the method is a static method (or should be) then that makes slightly better sense, but I would say that such a method does not belong inside the web part. It should be moved to a class library shared between the web part and the custom control.
a source to share
I agree with what John says and I would like to add the following ...
If your custom control is contained in your web part, you should be able to use the Parent property and climb up the hierarchy to find it. Another way to transfer communication between them is to register for events from one to another.
a source to share