Click here to Skip to main content
15,112,662 members
Articles / Web Development / ASP.NET
Posted 21 Aug 2007


17 bookmarked

ASP.NET Design Time Databinding Custom Controls

Rate me:
Please Sign up or sign in to vote.
3.86/5 (3 votes)
21 Aug 20071 min read
ASP.NET design-time databinding custom controls when they are inside of a databound composite control.


This article describes how to provide design-time databinding functionality to a custom web control when it is used in a data bound composite control's template. An example of this would be if you added your custom web control to an asp:Repeater's ItemTemplate.


When a custom web control is used inside of a databound composite control's template, the custom web control's designer is not used at design-time and databound properties are not set even though the Databind event is called. In order to set the custom web controls databound properties to correctly display the control at design time, a DataBindingHandler must be used.

Using the code

Shown below is sample code for a generic DataBinderHandler. This handler should correctly display any Bind("PropertName"), Eval("PropertyName"), or DataBinder.Eval(Container,"PropertyName") expression at design time.

In order to tell the designer to use a DataBinderHandler, you must add the DataBindingHandler attribute to the custom web control.

<Designer(GetType(InvoiceControlDesigner)), _
DataBindingHandler(GetType(DEDataBindingHandler)), _
ToolboxData("<{0}:InvoiceControl runat="server"></{0}:InvoiceControl>")> _
Public Class InvoiceControl
    Inherits System.Web.UI.WebControls.CompositeControl


End Class

Public Overrides Sub DataBindControl(ByVal designerHost As _
       System.ComponentModel.Design.IDesignerHost, _ 
       ByVal control As System.Web.UI.Control)

  Dim myIDataBindingAccesor As IDataBindingsAccessor = control
  If myIDataBindingAccesor.HasDataBindings Then
  For Each mydatabinding As DataBinding In _
    Dim pinfo As System.Reflection.PropertyInfo
    pinfo = control.GetType.GetProperty(mydatabinding.PropertyName, _
            mydatabinding.PropertyType, System.Type.EmptyTypes)
    If pinfo IsNot Nothing Then
        Dim MyValue As Object = Nothing
        Select Case mydatabinding.PropertyType.FullName
          Case "System.String"
            MyValue = "ABC"
          Case "System.Date"
            MyValue = #1/1/1970#
          Case "System.Integer", "System.Long"
            MyValue = 123
          Case "System.Single", "System.Double"
            MyValue = 12.34
          Case Else
            MyValue = "DataBound"
        End Select
          If control.BindingContainer IsNot Nothing AndAlso _ 
            TypeOf control.BindingContainer Is IDataItemContainer Then
             If mydatabinding.Expression.ToLower.StartsWith("bind(""") Or _
              mydatabinding.Expression.ToLower.StartsWith("eval(""") Or _
              mydatabinding.Expression.ToLower.StartsWith("databinder.eval(container") Then
               Dim StartPosition As Integer = mydatabinding.Expression.IndexOf("(")
               Dim FormatPosition As Integer
               Dim ExPrefix As String = ""
               If Not mydatabinding.Expression.ToLower.StartsWith(_
                          "databinder.eval(container") Then
                 ExPrefix = "DataItem."
                 StartPosition = mydatabinding.Expression.IndexOf(",", StartPosition) + 2
               End If
               FormatPosition = mydatabinding.Expression.IndexOf(",", StartPosition)
               Dim mydatabindingex As String = ""
               If FormatPosition > 4 Then
                 mydatabindingex = mydatabinding.Expression.Substring(StartPosition, _
                                   FormatPosition - StartPosition + 1)
                 Dim FormatString As String
                 FormatString = mydatabinding.Expression.Substring(FormatPosition + 2, _
                                mydatabinding.Expression.Length - FormatPosition - 4)
                 MyValue = DataBinder.Eval(control.BindingContainer, _
                           ExPrefix & mydatabindingex, FormatString)
                 mydatabindingex = mydatabinding.Expression.Substring(StartPosition, _
                                   mydatabinding.Expression.Length - StartPosition - 2)
                 MyValue = DataBinder.Eval(control.BindingContainer, _
                           ExPrefix & mydatabindingex)
               End If
             End If
          End If
        Catch ex As Exception
          Debug.WriteLine("DataBinderHandler Failed " & ex.ToString)
        End Try
        If System.Type.Equals(MyValue.GetType, mydatabinding.PropertyType) Then
          pinfo.SetValue(control, MyValue, Nothing)
        End If
      End If
  End If
End Sub
End Class

The ASP.NET code:

<asp:Repeater ID="Repeater1" runat="server" 
  DataMember="InvoiceList" DataSourceID="DEDataSource1">


<asp:Label ID="Label1" runat="server" 
  Text='<%# Bind("InvoiceID") %>'></asp:Label>

<DE:InvoiceControl ID="InvoiceControl1" runat="server" 
  Invoice='<%# Databinder.Eval(Container,"DataItem") %>' 
  InvoiceControlMode="ReadOnly" InvoiceControlStyle="Default" >

<InvoiceTableStyle CellPadding="3" 
  CellSpacing="0" Height="100%" Width="100%" />


Points of interest

It took a lot of digging before I was able to figure out why my custom control would not work once placed inside of a Repeater. Debugging revealed that the databound properties were not getting set but the databinding event was getting called. I also noted that at design time, the designer did not call the PreRender event. I am not sure why Microsoft did not include a generic DataBindingHandler to be used when there was not a DataBindingHandler attribute set.


  • Originally posted on 08/21/2007 by Andrew DeVries.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Andrew DeVries has been a .Net programmer since 2003. He currently works as a consultant designing custom software for windows as well as for the web. Currently he spends his days designing custom web controls and custom data sources.

Comments and Discussions

QuestionWorking on the same thing, having questions... Pin
Yannick.WILLI23-Aug-07 0:05
MemberYannick.WILLI23-Aug-07 0:05 
GeneralThanks Pin
merlin98121-Aug-07 8:45
professionalmerlin98121-Aug-07 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.