how to mimic partial view in asp.net? - asp.net

I'm working on an Asp.net website (it's been awhile) coming from an MVC background. How would I mimic partial views in Asp.net? Do I use web user controls or web part zones, or other?

ASP.NET is actually the base engine for both ASP.NET Webforms and ASP.NET MVC.
If you're used to MVC then you should really use ASP.NET MVC, where Partial Views are first class citizens.
Use ASP.NET MVC
To use one, you simply create a new MVC project, right click on the shared folder in the View folder, and select Add New View. From there you can select Partial View.
If you have to use Webforms
If you're stuck with webforms, then you use UserControls. The webforms model doesn't really translate well to the MVC paradigm, but UserControls are the closest things.
I think webparts are for Sharepoint only... ignore them.

User Controls are the ASP.NET WebForms equal of ASP.NET MVC Partial Views.

I would use Web User Controls. As far as I can tell they offer the same functionality. Plus the MVC implementation in ASP.NET uses them for partial views as well.

I assume you are using ASP.NET webforms and not ASP.NET MVC or this wouldn't be an issue. In webforms you can use UserControls (.ascx) to achieve a similar result.
One of the advantages of using user controls is you can interact directly with the details of the user control by exposing public properties and methods in the user control itself. You can make the user control have a lot of flexibility and have much of it's functionality defined by pure markup as well.
With partial views in MVC this is not really possible. I can't really think of anything you can't do with a user control that you can with a partial view in MVC. On the flip side there is ton of stuff a partial view can't do that a user control can.
Example of using a user control in a webforms project.
First add a reference to your user control at top of aspx page:
<%# Register Src="UserControls/PersonSearch.ascx" TagName="PersonSearch"
TagPrefix="uc1" %>
Then when you want to use it:
<uc1:PersonSearch hid="ucPersonSearch" runat="server">
</uc1:PersonSearch>
You can register events from you main page with the user control, expose properties, methods, etc...
For instance you could have a user control and register a notify event from your main page with the user control to get triggered once a person is selected. The user control just has to know about checking to see if a delegate has been registered and call it. This gives the container the ability to use the user control however it wants and leverage it's strength in unique ways.
It's more like a partial view on steroids but I 'partial' to user controls (pun intended) :)

Related

WebForms Transferring To MVC

I was wanting to ask the following about mvc to have a better understanding.
1 What is the difference between the way webforms and action controllers work.
2 How should one coming from a webforms background convert the page_load etc button clicks etc into mvc methods and events. Its this understanding that I am lacking.
3.How do i fill controls before I was used to setting the datasource but see allot of controls using the foreach on the front end is that really code separation?.
4 I will be developing a form designer in .net webforms I was used to using panels and loading controls but I see it will be neater in mvc by using partial views would that be my best course of action. I am a senior asp.net webforms developer with over ten years experience.
I have been watching plurisight videos but they center on using sql express not server.
A lot of help material you can get online about this topic.
Your first question about the way webforms and action controllers work -
In webform, you specify the code-behind file of your .aspx page and the code-behind file is the master of that page now. The browser hits the .aspx and the code behind file manages the work.
But in MVC, no view file is approached; the path is matched to the respective controller and action and the action handles it. Any controller can access the View of any other Controller. There are Shared Views which are common for every controller as well.
I strongly suggest you to read at this link and this codeproject article.
Some major points will be this :
You wont have the RAD (Rapid Application Development) environment i.e. the drag drop support for Controls, page viewer in case of Razor, etc.
You wont have the basic server controls Gridview, Repeater,etc in MVC. None of the controls in MVC are bind to any controller. You can neatly pick the desired elements using Javascript and play with them.
You get full control over the HTML
What I feel is that MVC is more flexible and jQuery-able as compared to Webforms
All the best!

What is ASP.NET WebForms equivalent of ASP.NET MVC's ViewData

What are the conventions used in ASP.NET WebForm for passing data to view from code behind?
In ASP.NET MVC for example ViewData is a key value collection or a strongly typed class object. So what do people do in case of ASP.NET WebForm.
I know we can create a property or member of a class or add stuff to Page.Items but what else besides that?
I think all the concepts of ASP.NET MVC do not map to ASP.NET Forms since they are two different paradigms of building web app.
In WebForms people mostly deal with controls and set their properties, they don't have to pass data to view as such. However if they do have to do so they use Page.Items or HttpContext.Current.Items or create Page properties that they access in views.
There is no direct equivalent of ViewData or ViewModel in WebForms that is used in practice. Page.Items is the closest thing.
I'm not sure there is a direct equivalent, but the "HttpContext.Current.Items" collection can be accessed from anywhere without having to pass the context (though it does make assemblies dependent on System.Web).
You can use ViewState.
View state is a repository in an ASP.NET page that can store values that need to be retained during postback. View state is typically used for page variables that must be retained rather than user or session data. For example, you can store information in view state that will be accessed during the page load event the next time the page is sent to the server
Please see details at: https://msdn.microsoft.com/ro-ro/library/ms227551(v=vs.85).aspx

MVC Custom Control?

I am trying to figure out how to use/create a custom control in ASP.NET MVC 2.
I created a custom control earlier and compiled it (ccontrol.dll), the control renders a div, textbox and a button + some javascript in order to post a comment on the website. It could be a static aspx page that i wanted to allow my visitors to add a comment to. I would then drag my control from the toolbar to the aspx page and run it, it would then render all the code needed on the webpage including fetching the data from a datasource and displaying that inside the div. The user could also just type in a comment and press the button to save it to the datasource.
Is this possible to convert to MVC 2? Any good tutorial that covers custom controls and MVC 2? (Ideally would be if the control could be made into a .dll file that i then could reuse on future webpages)
How do i write a custom control the mvc way? Any good tutorials on the topic?
You cannot design Custom Controls according the normal asp.net style because in Mvc there is no ViewState and there are no server side control events. Data are returned back to the server through a Model Binding process. The fact that rendering and filling data in are handled in separated pieces of code make difficult to implement complex server controls in Mvc.
However, I developed a theory, and also a toolset to make quite easily custom controls ina Mvc too in the full spirit of the Mvc paradigm i.e keeping separation of concerns between Views and Controllers. See My Codeplex project. There, you will find pointers to documentation and tutorials on my blog. If you need assistance feel free to contact me.
No it is not possible to use custom controls in ASP.NET MVC. you need to re-write in MVC way

ASP.NET: Webforms and MVC pattern

I have developed a webapplication in both ASP.NET MVC and ASP.NET Webforms and i'm wondering isn't Webforms following the rules of the MVC Pattern just the as ASP.NET MVC is?
I mean we have the .aspx file which holds the visual (HTML and JavaScript) and then the code behind file which controls the user interaction and data for the .aspx file. Then we could make a Repository lager for fetching and doing stuff with data.
Isn't this the same as following the rules of the MVC Pattern? View for visual, Controller for controlling user interaction and data for the Views and the Model fetching and doing stuff with data?
I know ASP.NET MVC and Webforms handles Postbacks and URL handeling differently, but im not comparing the two ASP.NET techniques, but the MVC Pattern in generele for the two techniques.
ASP.NET Webforms is definitely not following the MVC pattern.
In MVC you have three elements, the Model, the View, and the Controller.
In ASP.NET Webforms, you have your Page (codebehind and the markup are compiled into a single object, not seperate), and whatever data is being shown. You really have no controller. You make a requrest directly to the page rather than a controller and the page is responsible for both working with the data and rendering the page. Definitely not seperated like MVC would be.
Just the fact that you're not using the codebehind in your aspx page should give you a hint that there are some pretty substantial differences between MVC and webforms... You aren't using the codebehind, are you?
Not to mention how hard it is to test a Webforms page.... You are testing your code, right?
You can indeed use an MVC pattern with Web Forms, but all you are doing is adding additional classes or layers to the postback. The previous answers here are coming from a purist point of view that is more clear separations built into the technology. But, there is nothing preventing you from using different classes to represent the Model the View and Controller and only have your WebForms bind to the View. All of the communication is still done on postback, but it is still technically MVC and still the correct pattern to use.
Reference:
http://msdn.microsoft.com/en-us/magazine/ff955232.aspx

What is the difference between UserControl, WebControl, RenderedControl and CompositeControl?

What is the difference, what is the official terms, are any terms obsolete in ASP.NET 3.5?
UserControl: A custom control, ending in .ascx, that is composed of other web controls. Its almost like a small version of an aspx webpage. It consists of a UI (the ascx) and codebehind. Cannot be reused in other projects by referencing a DLL.
WebControl: A control hosted on a webpage or in a UserControl. It consists of one or more classes, working in tandem, and is hosted on an aspx page or in a UserControl. WebControls don't have a UI "page" and must render their content directly. They can be reused in other applications by referencing their DLLs.
RenderedControl: Does not exist. May be synonymous to WebControl. Might indicate the control is written directly to the HttpResponse rather than rendered to an aspx page.
CompositeControl: Inbetween UserControls and WebControls. They code like UserControls, as they are composed of other controls. There is not any graphical UI for control compositing, and support for UI editing of CompositeControls must be coded by the control designer. Compositing is done in the codebehind. CompositeControls can be reused in other projects like WebControls.
You've forgotten the ServerControl.
In my understanding it is like that:
There are only two different kind of controls: UserControl and ServerControl
CompositeControls are kind of "advanced" UserControls. Find some more info on Scott Guthries Blog.
All of them are WebControls (because they are all derived from System.Web.UI.Control)
They are all rendered in any way so i would like to see them all as rendered controls.
From MSDN:
User Control
In ASP.NET: A server
control that is authored declaratively
using the same syntax as an ASP.NET
page and is saved as a text file with
an .ascx extension. User controls
allow page functionality to be
partitioned and reused. Upon first
request, the page framework parses a
user control into a class that derives
from System.Web.UI.UserControl and
compiles that class into an assembly,
which it reuses on subsequent
requests. User controls are easy to
develop due to their page-style
authoring and deployment without prior
compilation.
Server control
A server-side component
that encapsulates user interface and
related functionality. An ASP.NET
server control derives directly or
indirectly from the
System.Web.UI.Control class. The
superset of ASP.NET server controls
includes Web server controls, HTML
server controls, and ASP.NET mobile
controls. The page syntax for an
ASP.NET server control includes a
runat="server" attribute on the
control's tag. See also: HTML server
control, validation server controls,
Web server control.
Like Web Forms, user controls can be created in the visual designer or they can be written with code separate from the HTML. They can also support execution events. However, since Web user controls are compiled dynamically at run time they cannot be added to the Toolbox and they are represented by a simple placeholder when added to a page.
This makes Web user controls harder to use if you are accustomed to full Visual Studio .NET design-time support, including the Properties window and Design view previews. Also the only way to share the user control between applications is to put a separate copy in each application, which takes more maintenance if you make changes to the control.
Web custom controls are compiled code, which makes them easier to use but more difficult to create. Web custom controls must be authored in code. Once you have created the control you can add it to the Toolbox and display it in a visual designer with full Properties window support and all the other design-time features of ASP.NET server controls. In addition you can install a single copy of the Web custom control in the global assembly cache and share it between applications, which make maintenance easier.
Contrary to Will's response, it is possible to reuse UserControls in other projects by referencing a web deployment project.
Since I don't have enough reputation yet to comment, I'll add this as an answer, but it refers to Will's answer above.
From the link you included:
Composite controls are the right tool to architect complex components in which multiple child controls are aggregated and interact among themselves and with the outside world. Rendered controls are just right for read-only aggregation of controls in which the output doesn't include interactive elements such as drop-down or text boxes.
I believe the documentation is refering to UserControls that have been created by overriding the Render method as Rendered Controls. Thus, it is not a separate type as the question implies, but a way of implementing a UserControl; a pattern.

Resources