When do you need .ascx files and how do you use them?

When creating a website, when would it be wise to use .ascx files? What exactly is .ascx and what is it used for? Examples would help a lot!

+76
Mar 07 2018-11-11T00:
source share
7 answers

The extension for User Control that you have in your project.

A user control is a kind of composite control that is very similar to an ASP.NET web page β€” you can add existing web server controls and layouts to a user control and define properties and methods for the control. You can then embed them in ASP.NET web pages, where they act as a unit.

Simply, if you want to have some functionality that will be used on many pages of your project, then you need to create a User or Composite control and use it on your pages. It just helps you keep the same functionality and code in one place. And make it reusable .

+81
Mar 07 2018-11-11T00:
source share

We mainly use user controls when we need to use similar functionality in different places of the application. Just as we use the master pages for the consistent appearance of the application, similarly to avoid repeating the same functionality and user interface throughout the application, we use usercontrols. There may be much more use, but I only know this ...

For example, suppose your site has 4 user levels, and for each user there are different pages in different directories with different access mechanisms. Suppose you request address information for all users, and then create address fields such as Street, City, State, Postal Code, etc. On each page. This will be a recurring job. Instead, you can create it as an ascx file (ext for a user control), and in this control place the necessary user interface and business code to add / update / delete / select the role of the address, and then simply specify the entire necessary page.

So, with thoughtful user controls, you can avoid repeating code for each role and creating a user interface for each role.

+11
Mar 07 '11 at 10:45
source share

Ascx files are called User Controls and are designed to be reused and to simplify complex ASPX pages (open part of the page). They can also be useful for something called donut caching , that is, when you want to cache a specific part of a page.

+9
Mar 07 2018-11-11T00:
source share

If you have a block of code + html that is displayed on several pages and does not depend on this page (for example, a block of the latest news), you can copy / paste the code on each page.

However, it’s better to put this code in your own block and just include this block on every page you need. This "block" is an ascx file.

+2
Mar 07 2018-11-11T00:
source share

ASCX files are a server-side web application platform designed for web development to create dynamic web pages. They are like DLL codes, but you can use TAGS there. You can write them once and use them anywhere on ASP pages. If you have a file named "Controll.ascx", its code will be named "Controll.ascx.cs". You can embed it in an ASP page to use it:

+1
Dec 03 '13 at 8:44
source share

Another use of .ascx files, they can be used to cache partial pages on ASP.NET pages. We need to create an ascx file and then move the controls or part of the page that we need to cache into this control. Then add the @OutputCache directive in the ascx control and it will be cached separately from the parent page. It is used when you do not want to cache the entire page, but only a certain part of the page.

+1
Dec 18 '13 at 20:24
source share

When you build an asp.net base website using webcontrols, it is a good idea when you want to be able to use your controls in more than one place on your website. Separating code from ascx files will contain controls that are used to display the layout, cs files belonging to ascx files will contain code that populates these controls.

For some basic understanding of usercontrols, you can try this website.

-2
Mar 07 2018-11-11T00:
source share



All Articles