views:

157

answers:

2

I've followed the instructions in this MSDN article: http://msdn.microsoft.com/en-us/library/dd206945.aspx

Is it possible to call a method that is in the myWebPart.cs file, from the MyUserControl.ascx file? I don't seem to have intellisense of the methods in the myWebPart.cs unless I do:

myWebpart mywbprt = new myWebpart();
mywbprt.myMethInWebPartcs();

However that gets the error, and does not compile:

Error   2 'myWebpart' is a 'namespace' but is used like a 'type'

The myWebPart.cs and MyUserControl.ascx share the same namespace, and I thought that would be enough to call the methods in myWebPart.cs in the usercontrol, but apparently not?

Am I missing some intricacy of SharePoint?

+1  A: 

First of all, it looks like the namespace that myWebPart is in has the same name as the web part. You should change that. You will then probably not have the compile-time error.

You will have a runtime error. If this method you want to call interacts with the runtime state of the web part, then it should not be called from outside the web part, most likely.

More to the point, the user control needs to find and call on the particular instance of the web part that is currently active, not create a new instance, which will have nothing to do with the instance that was already running.

If the method is a static method (or if it should be), then this makes somewhat better sense, but I would argue that such a method does not belong inside the web part. It should be moved to a class library shared by the web part and by the user control.

John Saunders
I see your point. Could you quickly run down how I'd do the class library? I've added a class library to the solution containing both the WebPart and the UserControl, but I'm not sure how to call the classes within the class library. :)
program247365
Also, I've changed the namespace, to something other than the name of the webpart.
program247365
I've implemented a class library, and this answer has answered my question. It seems more clean to have a class library involved in this kind of project. So I added a class library to my webpart solution, and then added the references to the class library in the webpart and usercontrol, and this seemed to do the trick. Thanks all, for you answers/comments!
program247365
Glad it helped. Sorry I didn't see your earlier comments.
John Saunders
+1  A: 

I agree with what John says, and I'd like to add the following...

If your user control is contained within your web part, you should be able to use the Parent property and go up the hierarchy to find it. Another way of handing communication between the two would be to register for events from one to the other.

Kirk Liemohn
What would the code look like if I wanted to take the route of 'registering events'?
program247365
If you register events, don't you also have to somehow get a reference to the instance of the web part? If you can find it in the hierarchy, then it would work, but wouldn't otherwise...
George Durzi
Yes, if you do register events you would have to find it in the hierarchy. These would be custom events defined either on the web part or the user control depending on who you want to depend on the other. For example, your user control could define some custom events and when the web part adds the user control to its controls collection it could also register for the event. That would be one way for your user control to communicate to your web part without the user control having any knowledge of the web part. See http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx for help.
Kirk Liemohn
This does seem like a good answer as well, but I think the class library makes more sense for my situation. Thank you very much for your responses.
program247365