如何在 MVC 项目的代码隐藏中访问服务器端控件?
首先,我要声明的是,我知道在 MVC 中访问视图中的服务器端控件是不受欢迎的。 但是,我需要根据我的情况(据我所知)。 这是我的故事。 :)
我有一个在我的网络应用程序中使用的第三方控件。 我目前的任务是将我们的 WebForms 解决方案移植到 MVC。 这个特定的第三方 Web 控件需要 WebForms 架构,因此我希望仅使用我的 WebForms 项目中的相同代码。
我的初始方法是拥有两个网站(一个 WebForms 网站和一个 MVC 网站),然后从 MVC 端使用 iFrame 链接这两个网站。 虽然我知道这会起作用,但仅仅使用这个特定的控件就有点矫枉过正了。 经过更多研究后,我发现我可以“混合男孩”并使用MVC 项目内的 WebForms 架构。 因此,我采取的新方法是仅将使用此第三方控件的页面复制到特定目录(即“View\SomeDir\WebForms”),然后忽略该目录 不幸的是
routes.IgnoreRoute("View\SomeDir\WebForms\{*pathInfo}");
,当我将 ASPX 页面复制到我的 MVC 项目时,我发现 CodeBehind 不允许我访问页面上的控件。 以下是设置方式:
<%@ Register
Assembly="..."
Namespace="..."
TagPrefix="custom" %>
<custom:SomeControl ID="customControl" runat="server" />
这是我的隐藏代码的样子:
public class MyPage : Page
{
protected void Page_Load(object sender, EventArgs args)
{
Debug.WriteLine(customControl.ID); // <-- COMPILE ERROR: Cannot resolve symbol 'customControl'
}
}
不幸的是,我的项目甚至无法编译,因为我的 CodeBehind 中的每个控件都出现此错误。 将其他控件(即标签、面板、文本框等)转换为客户端标签不是问题,但我需要在后面的代码中访问此自定义控件,以便我可以收听它的(服务器端) 事件并做出相应响应; 基本的 WebForms 内容...
我想做的事情可能吗? 我认为可能有效的另一种方法是在控制器类中初始化并捕获服务器端事件。 但是,我想避免序列化视图中控件的状态,只是将其传递给控制器并返回(如果可能的话)?
预先感谢您的任何建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
碉堡了!! 我在发布此内容后 10 分钟找到了解决方案。
解决方法是右键单击我的项目并说“转换为 Web 应用程序”,然后将我的 CodeBehind 类更改回“部分”。 我的所有控件现在都可以通过后面的代码访问。
感谢您的阅读,我希望我的问题/答案有一天能对其他人有所帮助。 :)
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. :)
我不认为将某些内容移植到 MVC 只是为了将其中一半保留在传统 WebForm 中有什么意义。
这种方法意味着任何未来的开发人员都需要熟悉这两种技术,并且在我看来并不干净。
我会选择其中之一,并尽量避免将两者混合。
I do not see the point in porting something to MVC just to keep half of it in traditional 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.