User control to wrap existing code - how to do it?

Posted on 2009-06-29
Last Modified: 2013-12-26
Hi there,

We have an AJAX enabled .NET web site throughout which we use a customised GridView control. This is a class which is inherited from the standard GridView (System.Web.UI.WebControls.GridView). We would now like to wrap an AJAX Update Panel around each use of this custom GridView.

We could go through each page and insert the appropriate markup for the Update Panel whereever we use our custom web control, but I would like a more elegant (and less time consuming) solution.

I can't just change the existing custom web control, because it isn't an ascx and therefore it has no markup of its own. Similarly, I can't render an AJAX update panel whenever the custom gridview is rendered without doing all the essential AJAX plumbing that goes with it

I thought about a new ascx user control, but I can't find a way to wrap this around existing markup. Am I missing something horribly obvious here?

Any help greatly appreciated.


Question by:martyn_bannister
  • 2
LVL 12

Expert Comment

ID: 24741208
What you are asking to do is "how to do i create a composite control without creating a composite control" :)

All I can think of is perhaps override the CreateChildControls event.  This is where the server control builds all the internal controls and adds them to its controls collection.

So perhaps, let the grid build its child controls, but then remove them and add them to your own update panel, and then add the update panel back to the controls collection.  Pseudo code below, just thinking off the cuff
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
public class Class1 : GridView
    protected override void CreateChildControls()
        UpdatePanel panel = new UpdatePanel();
        panel.ID = "GridUpdatePanel";
        while (this.Controls.Count > 0)
            Control C = this.Controls[0];

Open in new window

LVL 12

Expert Comment

ID: 24741220
and actually i think the correct place to add back is the

Accepted Solution

martyn_bannister earned 0 total points
ID: 24743308
Hi wht1986,

Thanks for trying to help, but finally tracked down the solution I needed. It is a "Templated Control". This is basically a usercontrol which exposes a property based on the ITemplate interface. This property then allows a consumer of the ASCX to place any markup they want into the template. The ASCX has markup of its own which, in my case, is the UpdatePanel whose content is a a PlaceHolder. In the control's OnInit event, you read  the markup that the consumer has entered and use the "InstantiateIn" method to render it out to the PlaceHolder.

Beauty of this approach is that we can have any markup in and around the update panel in the user control and allow the consumer to place any markup inside the update panel. A further nicety is to expose the UpdatePanel properties (such as the Triggers) to the consumer of our control.

Solution attached, as well as code snippets.


''' ASCX Markup
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="RGVUpdatePanel.ascx.vb" Inherits="RGVUpdatePanel" %>
<asp:UpdatePanel ID="RGVUpdatePanel" runat="server">
        <asp:PlaceHolder ID="RGVPlaceHolder" runat="server"></asp:PlaceHolder>
''' ASCX.VB code
''' <summary>
''' User control that incorporates an UpdatePanel Control
''' </summary>
''' <remarks></remarks>
<PersistChildren(True)> _
<ParseChildren(True)> _
Partial Class RGVUpdatePanel
    Inherits System.Web.UI.UserControl
    ' Storage for user supplied markup
    Private _Content As ITemplate = Nothing
    ' Expose the markup storage to consumers of this user control
    <TemplateInstance(TemplateInstance.Single)> _
    <TemplateContainer(GetType(TemplateControl))> _
    <PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property ContentTemplate() As ITemplate
            Return _Content
        End Get
        Set(ByVal value As ITemplate)
            _Content = value
        End Set
    End Property
    ' Expose the UpdatePanel's Triggers to consumers
    ' of this user control
    ' The attribute makes intellisense aware of this property
    <PersistenceMode(PersistenceMode.InnerProperty)> _
    Public ReadOnly Property Triggers() As UpdatePanelTriggerCollection
            Return Me.RGVUpdatePanel.Triggers
        End Get
    End Property
    ' When this control builds, write out the user supplied markup
    '  into the PlaceHolder inside the UpdatePanel
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        If Not (_Content Is Nothing) Then
        End If
    End Sub
End Class
''' ASPX to utilise this control
<%@ Register TagPrefix="uc" TagName="MyUpdatePanel" Src="~/RGVUpdatePanel.ascx" %>
    <uc:MyUpdatePanel ID="Panel1" runat="server">
            <asp:AsyncPostBackTrigger ControlID ="Button1" EventName="Click" />
                <asp:Label ID="Label1" runat="server" Text="Label" Width="300px"></asp:Label>

Open in new window

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question