why not user control? - asp.net

why should be used use control template in asp.net? please, someone say me.
Can anyone tell me why I should use a Control Template over a UserControl in ASP.Net?

If you're familiar with MVC, a User Control is like the WebForms equivalent of a partial view.
Update (when I answered, the question asked when to use a User Control...):
A server control is appropriate when you want to bundle assets and functionality for wider distribution than a single project. It is more complex to develop a server control than it is a User Control, but a server-control allows you to completely encapsulate HTML, CSS, JavaScript, images, and server-side logic within a single DLL.
Conversely, a User Control is much easier to develop, but cannot contain assets such as external CSS/JS or images. User Controls are basically just partial bits of an ASPX page.
Unless you know that you need the features of a server control, I would always recommend a User Control for simple de-duplication of content that's repeated in multiple ASPX pages.

A User Control enables you to create your own custom control modules. They're useful because they are reusable, meaning you don't have to duplicate a lot of code. You can embed them in ASP.net pages, and you can define methods and properties for them.

Related

Is there a way to redirect or override an existing ASCX?

I am working on a ASP.NET site that uses several ascx files scattered throughout the web project, I want to add my ascx files but keep the existing code-behind file. I also want to enable the user to easily switch between the mine and the existing ones. Ideally, I want to enable the user to install the ascx files with minimal hassle without changing any code or going through some complex error-prone process. (The goal is to provide a change of the markup code)
ASP.NET provides ControlAdapters, a mechanism that allows a server control to be customized through adapters that map to a server control. I was wondering if such a mechanism exists for user controls, if not, are there any features or a work-around that provides the same results.
EDIT:
I discovered a "feature" that's not well documented that may allow me to do what I want. It's called tagMapping. It is supposed to work with server controls, but it's failed to work with UserControls.
You can add a panel to where you want to display these user controls. Then, load user controls dynamicly how you wish on backend.
UserControl ctrl = (UserControl)this.LoadControl("/UserControls/"
+ whichone + "/.ascx");
yourpanel.Controls.Add(ctrl);

Dynamic User Controls in asp.net

Hello I am trying to lear how to create Dynamic User Controls in asp.net.
I just know that this type of controls are created or loaded at run time.
Someone knows a good tutorial about this topic?
thanks in advance,
The best thing you can learn about dynamic controls in ASP.Net webforms is how to avoid them. Dynamic controls in asp.net are filled with pitfalls. I almost always recommend one of the following alternatives:
Place a reasonable fixed number of controls on the page, and then only show the ones you need.
Figure out the source for the dynamic controls and abstract it out to a datasource (array, ienumerable, list, etc) that you can bind to a repeater, even if it's just a call to Enumerable.Range().
Build a user control that outputs the html you want, bypassing the entire "controls" metaphor for this content.
If you really must work with dynamic controls, it's important to keep the stateless nature of http in mind, along with the asp.net page life cycle. Each adds it's own wrinkle to making dynamic controls work: the former that you need to create or recreate the controls every time you do a postback, and the latter that you need to do this before hitting the page load event - usually in page init or pre-init.
Typically what people are talking about here is the dynamic instantiation and addition of a control to a placeholder.
For example
Control ControlInstance = LoadControl("MyControl.ascx");
myPlaceholder.Controls.Add(ControlInstance);
The above instantiates MyControl.ascx and places it inside of a placeholder with id of myPlaceholder.
I agree with #Joel by knowing the page lifecycle, the stateless nature in mind etc it is possible to avoid the pitfalls. The main things to watch out for, which I have had to do, are:
Page_Init – initialise the controls that are on the page here as they were the last time you rendered the page. This is important as ViewState runs after Init and requires the same controls initalised the same way as the way they were previously rendered. You can load the control using the code from #Mitchel i.e.
Control ControlInstance = LoadControl("MyControl.ascx");
myPlaceholder.Controls.Add(ControlInstance);
Page_Load – Load the content of the controls in here as you would with any control that isn’t dynamically loaded. If you have kept a reference to them in your page_init they will therefore be available here.
Keeping to this structure I haven’t had too much difficulty as this is appears to be the way that ASP.NET was designed to work, even if all the samples on MSDN don’t do it this way. The biggest thing that you then have to watch is tracking what state the page was in in regard to the controls that you have had rendered.
In my case it was take the section number of the multipage survey and reload the questions from the database, so all I had to do was track the currently rendered section number which wasn’t difficult.
Having said all that if you are using dynamic controls just to show and hide different views of the same screen then I suggest you don’t use them. In this case I would much rather use either user controls (with the inappropriate ones hidden), placeholders to mark areas that aren’t to be rendered yet, or separate pages/views etc. as that way you keep the pages to a single responsibility which makes it easier to debug and/or get useful information from the user about which page they were on.
The Microsoft article is very good, but the best article that I have been read is in the bellow link:
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
If you are really interested in ASP.NET Web Forms dynamic controls, I recommend that you study the DotNetNuke CMS Portal. DotNetNuke is one of the best cases using dynamic controls as your core feature to build dynamic portals and pages using ASP.NET Portals. It is free for download in www.dotnetnuke.com.
I hope it helps

How to stop Pages becomming too bloated in webforms

I have an ASP.net webforms app that connects to a web service, All the functionality is on one page that has lots of difference states and I use a multi view to display the correct html depending on the current state.
The problem is the page is huge and unweildly. The code behind isn't so bad but the aspx page is just out of control.
I would like to have a seperate page for each possible state but can't find a good way to move and pass data between pages. Are there any patterns or practices that I can implement that can help with this? And if having a page for each state is not the way to go what else can I do?
I have to stick with the webforms platform :( so i can't move to MVC.
You need to look into Cross Page Posting. This will allow you to postback to a different url and still have access to the previous page's controls. It will make the separation of your different views much easier to manage.
You could create a custom control for each of your states and then programmatically instantiate the appropriate control and populate the properties at runtime.
Use user controls or custom controls to reduce the page weight and improve maintainability of your web forms.

ASP.Net Custom controls vs. user controls: Are these two the same

If they are different, under what circumstances should we decide to use either approach?
Also, what is the advantage of ascx over aspx?
user controls are a form of custom control, that gives you a visual designer. They are ideal for use when you want a reusable control within the same web site. (It is possible to create and package user controls as seperate assemblies but that is beyond the scope of this question and I know has been asked on SO).
A custom control is typically used to refer to a Web Control, or a Composite Control which is specialized form of a web control. These controls have no designer and are usually implemented in seperate projects from your web allowing them to be reused accross many sites.
Now your second question, ASCX and ASPX are two different things. ASCX is the extension for a User Control, where as ASPX is an ASP.Net Page. You cannot use an ASCX by itself it must be placed onto an ASPX or Master page.
One way I like to use user controls is I have for example a very complex page which have 7 tabs, 5 of those have grids, of those grids three of them are identicle. Well what I can do is create a seperate user control for the content of the tabs, this now reduces the code I need down significantly (Since three grids are identicle except for the data).
Further more it allows multiple to work on various parts of the page, and it helps me keep everything straight since I am reducing the complexity of the page. You do not use User Controls instead of Pages, you use them in conjuction with a page.
Edit
You do not use ascx over aspx. You use ascx to complement. For example on my site every page has the same footer, but I don't want every page to derive from a single master page. I can create my footer as an acsx control and put it in each of my master pages.
Another example, I have a form that lets a user enter three different date ranges. (And we have other forms). So I put the logic to enable a calender button, and a text box that when clicked on opens up the calender, in a user control. I can then reuse that user control in all my aspx pages.
Custom controls are control build entirely in code. The pro is that you can put them in libreries, add an icon to the toolbox and other fine control.
User controls are more easy to do, and in general is a way to encapsulate things to simplify other pages or when you need to use the same markup in several pages.
The advantage of controls over regular aspx pages is that you only need to do some part of the markup (not the whole page)
User Controls/Composite controls
User controls that are made up of other ASP.Net or custom controls. They are usually quite straight forward and can be created quite quickly. They Are generally not shared across projects however there are some tricks that can allow you to do this.
Custom controls
Custom controls are controls that you implement the UI by creating everything from the HTML output to design time support. Custom controls take much longer to make. You must use either Web.UI.Control or inherit from a sub control (Textbox for example). Custom controls are compiled to binary format to allow them to be distributed more easily. Since they are compiled they can be referenced from the toolbox in visual studio.
There are 2 main advantages to using a control in an aspx page. Encapsulation of logic and reuseability.

asp.net : is it possible to Split large ASP.NET pages into pieces?

Is it possible to split large ASP.NET-pages into pieces? JSP has the jsp:include directive. Is there any equivivalent in ASP.NET
I'm not interested in reusing the pieces. I just want to organize the HTML/ASP code.
Isn't User Controls and Master Pages overkill for doing this?
The MasterPage model and UserControls are the two out-of-box solutions to this.
<!--#include file="inc_footer.aspx"-->
If it's only ASP.net, then User Controls are an excellent way of splitting up pages and reusing code, as John Rudy suggests.
If any of your user controls use Javascript, one big gotcha to watch out for is to use the ClientID to refer to that control. When you add a user control to a page, ASP.NET mangles the ID to prevent collisions. The client ID is guaranteed unique.
You should take a look at ASP.NET User Controls which allow you to encapsulate UI functionality into smaller more manageable chunks.
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx
If you are talking about specific pages (as opposed to User Controls which are more intended for multi-page reuse than anything else), you could also use Partial Classes.
AFAIK in ASP.NET 2.0 and above ASPX pages are already Partial Classes by default (with all control declaration in the designer.aspx file).

Resources