Programming ASP.NET: Chapter 14: Custom and User Controls. Dynamic User Control, Ajaxify Your Controls. Introduction This article describes a control which can host a UserControl. It allows to Ajaxify a user control without any code change. The main benefit is that when refreshing its content, it does not instantiate the full page but only the contained user control. Most of the ASP.NET features (at least the ones that I tested) are supported: viewstate, controlstate, postback events, validators... Moreover, one of the cool features of this control is allowing UserControl to do cross-domain Ajax postback. Background When I wanted to put some Ajax in my ASP.NET project, I started by looking at the Microsoft Ajax Toolkit. I could have used some webservice to update my content, but I don't like to output HTML from a C# function and it's not easy to design content this way. The main difficulty was to allow a developer to Ajaxify a UserControl without any code change.
Implementation Here is a quick overview of how the Control is implemented and how it works: Server Side Client Side Limitations History. Understanding Interfaces and Their Usefulness. By Tim Stall Introduction Many large Web applications in existence make use of User Controls, which are a unit of user interface reuse in ASP.NET. Common UI pieces - such as menus, page headers or footers, and so on - can be easily encapsulated into a User Control for reuse on numerous other pages. In their simplest form, User Controls are statically added to a Web page, dragged on from the Solution Explorer in Visual Studio .NET. However, User Controls can also be dynamically added to a page, a technique commonly used for providing customized user interfaces. When multiple developers start building a large scale ASP.NET application that utilizes dynamically-added User Controls, the site's design often suffers from two common problems: Quality control, and Flexibility One reason quality control suffers is because it is natural for developers to give the functionality of their components different method names.
Working with Dynamic User Controls - The Non-Interface Solution Conclusion. Mastering Page-UserControl Communication. Table of Contents Introduction In the previous Mastering article (Mastering ASP.NET DataBinding), we took a detailed look at databinding - one of the most asked about topics in the newsgroups. Today, we continue the series by answering another very common question: how to maximize the communication between a page and its user controls.
The actual questions asked typically don't include the words "maximize the communication" in them, but are more along the lines of: How to access values from a page in a user control? The goal of this tutorial isn't only to answer these questions, but more importantly to build a foundation of understanding around these answers to truly make you a master of page-user control communication. Understanding the Basics Before we can answer the above questions, two basic concepts should be understood. A page is a class This is probably something you already know, but each code-behind file you create is actually compiled into a class. So what does that really mean? Calling Method in Parent Page from User Control. In ASP.Net, we develop custom user control as a reusable server control independent of any containing parent aspx page.
User control has its own public properties, methods, delegates, etc that can be used by parent aspx page. When a user control is embedded or loaded into a page, the page can access public properties, methods, delegates, etc that are in user control. After loading the user control, there a situation may arise like calling methods in page itself. But when a user control is developed, it has no knowledge of containing page. So it becomes a trick to call the page method. In .Net, Delegate class has one method DynamicInvoke. DynamicInvoke method is used to invoke (late-bound) method referenced by delegate.
First create a user control called CustomUserCtrl. Then add this user control into an aspx page. BtnMethodWithParam and BtnMethowWithoutParam are two different buttons on the user control that are invoking the methods in the parent page.