User-1304029052 posted
I'm new to creating custom controls to bear with me....
I have a control inherited from datalist because I want all the same functionality but with a couple of extra properties.
I have a method that goes off and gets the data inside the control (instead of using a separate datasource ID). It's reading it from XML into a datatable.
If on the code behind of the aspx page I put the control on I put a thecontrol.databind() in the page load, all works well.
However I'd rather not have to do this, instead the control should bind itself.
I gather it's something to do with the order of events but I'm not sure what to do about it.
I don't want to reinvent the datalist wheel, just add a couple of properties and get it's own data
Sample code (some code removed for clarity)
''' <summary>
''' Server control to place content from a Region onto the web page
''' </summary>
<DesignerAttribute(GetType(RegionDesignTime)), DefaultProperty("Text"), _
ToolboxData("<{0}:ctrlRegion runat=server RegionID=1 RepeatLayout=Flow><HeaderTemplate></HeaderTemplate><ItemTemplate><%#DataBinder.Eval(Container.DataItem, ""region_content"")%></ItemTemplate></{0}:ctrlRegion>"), _
Bindable(True)> _
Public Class ctrlRegion
Inherits System.Web.UI.WebControls.DataList
#Region "Fields"
Private _RegionID As Int32 = 0
Private _HTMLContent As String = String.Empty
#End Region
#Region "Properties"
''' <summary>
''' The Region to retrieve from the datastore
''' </summary>
<Bindable(True), Category("WCE"), DefaultValue("0"), Localizable(True), Description("Content region")> _
Property RegionID() As Int32
Get
Return _RegionID
End Get
Set(ByVal value As Int32)
If value = 0 Then
Throw New InvalidProgramException("The RegionID of the Region control must be greater than 0")
End If
_RegionID = value
End Set
End Property
''' <summary>
''' Holds the HTML content of the region ready to be displayed or saved to the datastore
''' </summary>
Property HTMLContent() As String
Get
Return _HTMLContent
End Get
Set(ByVal value As String)
_HTMLContent = value
End Set
End Property
#End Region
#Region "Methods"
Public Sub New()
Retrieve(RegionID)
End Sub
''' <summary>
''' Retrieves the Region content from the data source into its HTMLContent property
''' </summary>
Public Sub Retrieve(ByVal Region_id As Integer)
Dim dtRegions As New DataTable("dtRegions")
' Goes off and gets the data into dtRegion here
'
'
'
Dim query = _
From tbRegion In dtRegions.AsEnumerable() _
Where tbRegion.Field(Of Int32)("Region_id") = RegionID _
Select tbRegion
Dim view As DataView = query.AsDataView()
MyBase.DataSource = view
MyBase.DataBind()
DataBind()
End Sub
#End Region
End Class
''' <summary>
''' Design time representation of the control
''' </summary>
Public Class RegionDesignTime
Inherits ControlDesigner
Public Overrides Function GetDesignTimeHtml() As String
Dim designTimeHtml As String = ""
Dim myControl As ctrlRegion = CType(ViewControl, ctrlRegion)
designTimeHtml = String.Format("Region : {0}<br />", myControl.RegionID)
Return designTimeHtml
End Function
End Class