I have created two webparts via Visual Studio. After taking the connections option in the SharePoint UI, I'm able to connect between webparts. Is there any way to set the connections completely in the visual studio code itself ?
In SharePoint, a web part can be 'connected' to another web part to provide some information at run-time. The Provider web part pushes out the contracted information and the Consumer web part is set up to receive and consume the shared information.
This functionality lends to setting up a combination of simple web parts which may have been developed separately, into a functional and re-usable solution.
a. This custom interface will define the data to be shared from the Provider to the Consumer web part.
(2) Provider web part
a. Inherits from Microsoft.SharePoint.WebPartPages.WebPart
b. Implements a data interface - this interface is a definition of the data that will be shared from the Provider web part to the Consumer web part.
c. Provide mechanisms for user selection of the interfacing data
d. Provide a ConnectionProvider method which will expose the interfacing data to the Consumer web part.
(3) Consumer web part
a. ConnectionConsumer method which will receive the interfacing data
b. 'Consume' the interfacing data and provide enriching details to the user.
you can also do it using SPLimitedWebPartManager to connect the Web parts with ASP.NET style interfaces.
Follow the steps in the below URL to do your requirement,
I haven't tried with Visual Studio, but the following approach will be helpful to do your requirement,
We can create a static Web Parts connection between two server controls by declaring the connection in page persistence format. A static connection becomes a permanent object on a Web page, like a declared control. All users can see a static connection (because it is a shared object), and they can never delete the connection object, although they can disconnect it if provided with the user interface (UI) options for doing so. Static connections are a good option when you want a connection that is always available to all users, and you do not want users to have the option of deleting it from the page.
Yes, You can connect your webparts programmatically, below are good links to achieve your goal: