简体   繁体   English

ASP.NET/UserControls:如何将“控制器”与“视图”分开?

[英]ASP.NET/UserControls: How do I separate the “controller” from the “view”?

Is it possible to separate the ascx ("view") and the ascx.cs ("controller") pars of a user control.是否可以将用户控件的 ascx(“视图”)和 ascx.cs(“控制器”)pars 分开。 Id like to move the controller part to App_Code to be resued while the view part is changed between projects?我想将 controller 部分移动到 App_Code 以在项目之间更改视图部分时重新使用?

Yes, write the codebehind in some service class which extends UserControl, and in your.ascx file inherit that class是的,在扩展 UserControl 的某些服务 class 中编写代码隐藏,并在 your.ascx 文件中继承该 class

<%@ Control
Language           = "C#"
Inherits           = "Project.Business.Service.MyControl"
%>

In regular ASP.NET, even if you separate the code-behind and the ascx - they are still tightly coupled.在常规 ASP.NET 中,即使您将代码隐藏和 ascx 分开 - 它们仍然是紧密耦合的。 It isn't a true "controller" (as separate from a view).它不是真正的“控制器”(与视图分开)。

If you want this purity, consider ASP.NET MVC, which (obviously) addresses this in a different way.如果您想要这种纯度,请考虑 ASP.NET MVC,它(显然)以不同的方式解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET应用程序中分离视图和控制器 - How to separate the view and the controller in an ASP.NET application 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? 如何从ASP.NET中的Jquery调用UserControls CodeBehind方法? - How to call a UserControls CodeBehind method from Jquery in ASP.NET? 如何将带有自己的控制器的局部视图添加到 asp.net mvc 4 中的另一个视图 - How do I add a partial view with its own controller to another view in asp.net mvc 4 ASP.NET MVC - 如何从视图中调用 Controller 方法以重定向到多个其他视图? - ASP.NET MVC - How do I Call a Controller Method from within a View to Redirect to Multiple Other Views? 我如何将数据发送到asp.net控制器,以便它可以返回视图? - How do i send data to asp.net controller so that it can return a view? Asp.Net MVC:如何获取当前控制器/视图的虚拟URL? - Asp.Net MVC: How do I get virtual url for the current controller/view? 如何在ASP.NET MVC 5项目中获得“添加控制器”和“添加视图”菜单选项? - How do I get the “Add Controller” and “Add View” menu options in my ASP.NET MVC 5 project? ASP.NET MVC如何将控制器操作指向其他视图? - ASP.NET MVC How do I point a controller action to a different view? 如何使用外键关系将数据从视图返回到 asp.net 中的 controller? - How do you return data from a view to controller in asp.net using foreign key relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM