How can I access the server control in code in my MVC project?

First of all, let me say that I know that access to server-side controls in my view is not approved in MVC. However, I need in my situation (as far as I see). Here is my story. :)

I have a third-party control that I use in my web application. I am currently tasked with transferring our WebForms solution to MVC. This particular third-party web control requires the WebForms architecture, so I just want to use the same code from my WebForms project.

My initial approach was to have two websites (WebForms website and MVC website) and then link them using iFrames from the MVC side. Although I know this will work, it was a little redundant to use this special control. After doing more research, I discovered that I can โ€œmix boysโ€ and use the WebForms architecture inside an MVC project. Therefore, the new approach that I used is to only copy pages that use this third-party control into a specific directory (for example, "View \ SomeDir \ WebForms"), and then ignore this directory in my global.asax file so that the routing system MVC did not pick it up:

routes.IgnoreRoute("View\SomeDir\WebForms\{*pathInfo}");

Unfortunately, when I copied the ASPX page to my MVC project, I found that CodeBehind was not giving me access to my control on the page. Here's how to do it:

<%@ Register 
  Assembly="..." 
  Namespace="..." 
  TagPrefix="custom" %>

<custom:SomeControl ID="customControl" runat="server" />

This is what my code 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 doesnโ€™t even compile because I get this error for every control in CodeBehind. Itโ€™s not a problem to convert other controls (e.g. labels, panels, text fields, etc.) into client tags, but I need to have access to this custom control in my code so that I can listen to it (on the server side) of events and respond accordingly; WebForms basic materials ...

, ? , might, . , , ?

!

+5
2

!! 10 , .

- " -" CodeBehind "". .

, , / - .:)

+5

- MVC, WebForms.

, , , .

.

0

All Articles