First and foremost let me state that I know that accessing server side controls in my View is frowned upon in MVC. However, I need to in my situation (as far as I can see). Here is my story. :)
I have a 3rd party control that I'm using in my web application. I've currently been given the task to port our WebForms solution to MVC. This particular third party web control requires the WebForms architecture, so I'm looking to just use the same code from my WebForms project.
My initial approach was to have two websites (a WebForms site and an MVC site) and then linking the two using iFrames from the MVC side. While I know that would work, it was a bit of an overkill just to use this particular control. After doing more research, I discovered that I can "mix the boys" and use the WebForms architecture inside an MVC project. Therefore, the new approach that I've taken is to only copy over the pages that use this 3rd party control into a specific directory (ie. 'View\SomeDir\WebForms') and then ignore that directory in my global.asax file so that the MVC routing system does not pick it up:
routes.IgnoreRoute("View\SomeDir\WebForms\{*pathInfo}");
Unfortunately, when I copied over the ASPX page to my MVC project, I have discovered that the CodeBehind does not grant me access to my control on the page. Here is how things are set up:
<%@ Register
Assembly="..."
Namespace="..."
TagPrefix="custom" %>
<custom:SomeControl ID="customControl" runat="server" />
Here is what my code behind looks like:
public class MyPage : Page
{
protected void Page_Load(object sender, EventArgs args)
{
Debug.WriteLine(customControl.ID); // <-- COMPILE ERROR: Cannot resolve symbol 'customControl'
}
}
Unfortunately my project won't even compile because I get this error on every control in my CodeBehind. It's not a problem to convert the other controls (ie. labels, panels, textboxes, etc..) to client tags, but I need to have access to this custom control in my code behind so I can listen to it's (server-side) events and respond accordingly; basic WebForms stuff...
Is what I'm trying to do even possible? Another approach that I thought about that might
work is to initialize and trap the server side events in the controller class. However, I would like to avoid serializing the state of the control in the view, simply to pass it to the controller and back if possible?
Thanks in advance for any advice!