Custom Control Postback inside DataList *SOLVED*
Hi,
Im making some custom controls including a button that can be changed into Button, Linkbutton or Imagebutton by using the Type Param inside HTML Tags
The following code works fine as button on a page.
Code:
<HostingControls:Button ID="buttonAdd" Runat="server" Type="Link" Text="Add" />
The following code is the same button inside a Datalist.
The button doesnt forward the event to the datalist ItemCommand event. A normal ASP:Linkbutton does.
Code:
<HostingControls:Button CommandName="select" Runat="server" Type="Link" Text="Read more" />
<!--/ Old button: <asp:LinkButton Runat="server" CommandName="select">Read more</asp:LinkButton> /-->
This is the full source of the Custom Button Class.
Code:
<DefaultProperty("Text"), ToolboxData("<{0}:Button runat=server></{0}:Button>")> Public Class Button
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler
Public Event Click As EventHandler
Public Enum ButtonType
Button = 0
Link = 1
Image = 2
End Enum
Private varText As String
Private varCommandName As String
Private varCommandArgument As String
Private varImageUrl As String = ""
Private varType As ButtonType = ButtonType.Button
<Bindable(True), Category("Appearance"), DefaultValue("")> Property Text() As String
Get
Return varText
End Get
Set(ByVal Value As String)
varText = Value
End Set
End Property
<Category("Appearance"), DefaultValue("")> Public Property CommandName() As String
Get
Return varCommandName
End Get
Set(ByVal Value As String)
varCommandName = Value
End Set
End Property
<Category("Appearance"), DefaultValue("")> Public Property CommandArgument() As String
Get
Return varCommandArgument
End Get
Set(ByVal Value As String)
varCommandArgument = Value
End Set
End Property
<Category("Appearance"), DefaultValue("")> Public Property ImageUrl() As String
Get
Return varImageUrl
End Get
Set(ByVal Value As String)
varImageUrl = Value
End Set
End Property
<Category("Appearance"), DefaultValue(GetType(ButtonType), "Button")> Public Property Type() As ButtonType
Get
Return varType
End Get
Set(ByVal Value As ButtonType)
varType = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
Dim varPostbackString As String = "javascript:" & Page.GetPostBackEventReference(Me, "")
output.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
output.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID)
If Not (varCommandName Is Nothing) Then
output.AddAttribute("CommandName", varCommandName)
End If
If Not (varCommandArgument Is Nothing) Then
output.AddAttribute("CommandArgument", varCommandArgument)
End If
Select Case Type
Case ButtonType.Button
output.AddAttribute(HtmlTextWriterAttribute.Type, "button")
output.AddAttribute(HtmlTextWriterAttribute.Value, Text)
output.AddAttribute(HtmlTextWriterAttribute.Onclick, varPostbackString)
output.RenderBeginTag(HtmlTextWriterTag.Input)
output.RenderEndTag()
Case ButtonType.Link
output.AddAttribute(HtmlTextWriterAttribute.Href, varPostbackString)
output.RenderBeginTag(HtmlTextWriterTag.A)
output.Write(Text)
output.RenderEndTag()
Case ButtonType.Image
output.AddAttribute(HtmlTextWriterAttribute.Type, "image")
output.AddAttribute(HtmlTextWriterAttribute.Onclick, varPostbackString)
output.AddAttribute(HtmlTextWriterAttribute.Alt, Text)
output.AddAttribute(HtmlTextWriterAttribute.Src, EngineGlobals.DataUrl(varImageUrl))
output.AddAttribute(HtmlTextWriterAttribute.Border, "0")
output.RenderBeginTag(HtmlTextWriterTag.Input)
output.RenderEndTag()
End Select
End Sub
Protected Overridable Sub OnClick(ByVal e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.Empty)
End Sub
End Class