Wednesday, April 13, 2011

ASP.NET/UserControls: How do I sepparate the "controller" from the "view"?

Is it possible to sepparate the ascx ("view") and the ascx.cs ("controller") pars of a user control. Id like to move the controller part to App_Code to be resued while the view part is changed between projects?

From stackoverflow
  • Yes, write the codebehind in some service class which extends UserControl, and in your .ascx file inherit that class

    <%@ Control
    Language           = "C#"
    Inherits           = "Project.Business.Service.MyControl"
    %>
    
    Niels Bosma : How do I reference childcontrols of that control?
    Spikolynn : If in your .ascx file you have a control In control code define protected Label myLabel, then set myLabel.Text = "x" for instance.
  • In regular ASP.NET, even if you separate the code-behind and the ascx - they are still tightly coupled. 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.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.