如何在我的MVC项目中访问代码隐藏中的服务器端控件?

时间:2021-12-11 15:05:11

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. :)

首先,我要说明我知道在我的View中访问服务器端控件在MVC中是不受欢迎的。但是,我需要在我的情况下(据我所知)。这是我的故事。 :)

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.

我有一个第三方控件,我在我的Web应用程序中使用。我目前被赋予了将WebForms解决方案移植到MVC的任务。这个特定的第三方Web控件需要WebForms体系结构,因此我希望只使用WebForms项目中的相同代码。

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:

我最初的方法是拥有两个网站(一个WebForms网站和一个MVC网站),然后使用来自MVC端的iFrame链接这两个网站。虽然我知道这会起作用,但使用这个特殊控件只是有点过分。经过更多的研究,我发现我可以“混合男孩”并在MVC项目中使用WebForms架构。因此,我采用的新方法是仅将使用此第三方控件的页面复制到特定目录(即'View \ SomeDir \ WebForms'),然后忽略我的global.asax文件中的该目录, MVC路由系统没有拿起它:

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:

不幸的是,当我将ASPX页面复制到我的MVC项目时,我发现CodeBehind不允许我访问我在页面上的控件。以下是设置方式:

<%@ 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...

不幸的是我的项目甚至都不会编译,因为我在CodeBehind的每个控件上都出现了这个错误。将其他控件(即标签,面板,文本框等)转换为客户端标签不是问题,但我需要在我的代码后面访问这个自定义控件,这样我才能听到它(服务器端) )事件并作出相应的反应;基本的WebForms东西......

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!

提前感谢您的任何建议!

2 个解决方案

#1


5  

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

哇靠!!我在发布此内容后10分钟找到了解决方案。

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.

修复是右键单击我的项目并说“转换为Web应用程序”并将我的CodeBehind类更改回“部分”。现在可以通过后面的代码访问我的所有控件。

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

感谢阅读,我希望我的问题/答案有一天会帮助别人。 :)

#2


0  

I do not see the point in porting something to MVC just to keep half of it in traditional WebForms.

我没有看到将一些内容移植到MVC只是为了将其中的一半保留在传统的WebForms中。

This approach will mean any future developers will need to be familiar with both technologies and is not clean in my opinion.

这种方法意味着任何未来的开发人员都需要熟悉这两种技术,并且在我看来并不干净。

I would go with one or the other and try to avoid mixing the two.

我会选择其中一个,尽量避免混合两者。

#1


5  

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

哇靠!!我在发布此内容后10分钟找到了解决方案。

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.

修复是右键单击我的项目并说“转换为Web应用程序”并将我的CodeBehind类更改回“部分”。现在可以通过后面的代码访问我的所有控件。

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

感谢阅读,我希望我的问题/答案有一天会帮助别人。 :)

#2


0  

I do not see the point in porting something to MVC just to keep half of it in traditional WebForms.

我没有看到将一些内容移植到MVC只是为了将其中的一半保留在传统的WebForms中。

This approach will mean any future developers will need to be familiar with both technologies and is not clean in my opinion.

这种方法意味着任何未来的开发人员都需要熟悉这两种技术,并且在我看来并不干净。

I would go with one or the other and try to avoid mixing the two.

我会选择其中一个,尽量避免混合两者。