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

Comment Utility
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

Comment Utility
and actually i think the correct place to add back is the

Accepted Solution

martyn_bannister earned 0 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now