Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a server-side control in a codebehind in my MVC project?

Tags:

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!

like image 818
Luc Avatar asked Jun 18 '09 20:06

Luc


1 Answers

Holy crap!! I found the solution 10 minutes after I posted this.

The fix was to right click on my project and say "Convert to Web Application" and change my CodeBehind class back to a 'partial'. All of my controls are now accessible via the code behind.

Thanks for reading and I hope my question/answer will help somebody else one day. :)

like image 190
Luc Avatar answered Oct 13 '22 08:10

Luc