Bueno, por fin lo he solucionado. He cambiado radicalmente la lógica del funcionamiento del control personalizado y he hecho uso de base de datos y procedimientos almacenados en lugar de XML, aunque esto último, podría haber seguido siendo igual.
Primero el control, que en lugar de derivar de un AdRotator deriva de PlaceHolder, con un AdRotator como clase privada. No he hechos comentarios al código por lo que si alguien tiene alguna duda... que pregunte, por favor.
cpersAdRotatorFlash.vb
Código vb:
Ver originalImports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Namespace ControlesPersonalizados
''' <summary>
''' Control personalizado para soportar Flash y fechas de visualización en un control AdRotator.
''' </summary>
''' <remarks></remarks>
<DefaultProperty("Text"), _
ToolboxData("<{0}:AdRotatorFlash runat=""server""></{0}:AdRotatorFlash>")> _
Public Class AdRotatorFlash
Inherits System.Web.UI.WebControls.PlaceHolder
Private _maxWidth As Unit
Private _keywordFilter As String
Public Property MaxWidth() As Unit
Get
Return _maxWidth
End Get
Set(ByVal value As Unit)
_maxWidth = value
End Set
End Property
Public Property KeywordFilter() As String
Get
Return _keywordFilter
End Get
Set(ByVal value As String)
_keywordFilter = value
End Set
End Property
Private Class tmpAdRotator
Inherits AdRotator
Private _e As System.Web.UI.WebControls.AdCreatedEventArgs
Public ReadOnly Property EventArgs() As System.Web.UI.WebControls.AdCreatedEventArgs
Get
Return _e
End Get
End Property
Protected Overrides Sub OnAdCreated(ByVal e As System.Web.UI.WebControls.AdCreatedEventArgs)
_e = e
End Sub
End Class
Private Function DTBanners(ByVal Keyword As String, Optional ByVal IsDefault As Boolean = False) As DataTable
Dim da As New SqlDataAdapter("PA_Anuncios", conSQL)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@keyword", Keyword)
da.SelectCommand.Parameters.AddWithValue("@isDefault", IsDefault.GetHashCode)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count = 0 And IsDefault Then
dt = DTBanners(Keyword, True)
End If
Return dt
End Function
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim ar As New tmpAdRotator()
Dim dt As DataTable = DTBanners(KeywordFilter)
If dt.Rows.Count <> 0 Then
ar.DataSource = dt
ar.DataBind()
ar.Target = "_blank"
Me.Controls.Add(ar)
Dim Height As Unit = CInt(ar.EventArgs.AdProperties("Height"))
Dim Width As Unit = CInt(ar.EventArgs.AdProperties("Width"))
If MaxWidth.ToString <> "" Then
If ar.EventArgs.AdProperties("Width") > MaxWidth.Value Then
Height = (MaxWidth.Value * Height.Value / Width.Value) - 5
Width = MaxWidth
End If
End If
ar.Target = _
IIf(String.Format("{0}", ar.EventArgs.AdProperties("Target")) <> "", _
ar.EventArgs.AdProperties("Target"), "_blank")
If ar.EventArgs.ImageUrl.ToLower.IndexOf(".swf") <> -1 Then
Dim StringFormat As StringBuilder = New StringBuilder("")
StringFormat.Append("<object type='application/x-shockwave-flash' data='{2}' width='{0}' height='{1}'>")
StringFormat.Append("<param name='movie' value='{2}' />")
StringFormat.Append("<img src='{2}' width='{0}' height='{1}' alt='Publicidad' />")
StringFormat.Append("</object>")
Dim OutPutString As StringBuilder = New StringBuilder()
OutPutString.AppendFormat(StringFormat.ToString, _
Width, _
Height, _
ResolveUrl(ar.EventArgs.ImageUrl))
writer.Write(OutPutString.ToString)
Else
Me.Controls.Clear()
ar.Width = Width
ar.Height = Height
Me.Controls.Add(ar)
MyBase.Render(writer)
End If
End If
End Sub
End Class
End Namespace
Estructura de la tabla en SQL Server, "tblAnuncios":
Código TXT:
Ver original3 ID int 4 0
0 ImageUrl nvarchar 400 0
0 NavigateUrl nvarchar 400 0
0 AlternateText nvarchar 400 1
0 Keyword nvarchar 125 1
0 Impressions int 4 1
0 Width int 4 1
0 Height int 4 1
0 StartDate smalldatetime 4 1
0 EndDate smalldatetime 4 1
0 Target nvarchar 50 1
0 IsDefault bit 1 0
Procedimiento almacenado en SQL Server, "PA_Anuncios":
Código SQL:
Ver originalALTER PROCEDURE PA_Anuncios
(
@keyword nvarchar(125),
@isDefault bit = 0
)
AS
BEGIN
SELECT * FROM tblAnuncios
WHERE
Keyword = @keyword
AND
ISNULL(StartDate,0) <= CASE WHEN StartDate IS NULL THEN ISNULL(StartDate,0) ELSE GETDATE() END
AND
ISNULL(EndDate,0) >= CASE WHEN EndDate IS NULL THEN ISNULL(EndDate,0) ELSE DATEADD(DAY,-1, GETDATE()) END
AND
IsDefault = @isDefault
END
Y finalmente, la inserción del control en el HTML:
Código HTML:
Ver original<ControlesPersonalizados:AdRotatorFlash ID="AdRotatorFlash1" runat="server" KeywordFilter="rotatorioMenu" MaxWidth="190" />
¿Pegas y/o sugerencias?
Un saludo.