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

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

and actually i think the correct place to add back is the
martyn_bannisterAuthor Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.