Typecasting asp.net webControl to generic type with onClick event

I'm trying to find a way in which I can create a collection of various webControl's and then add a "onClick" event handler to these various controls, i've tried creating a reimplementation of "webControl" with a registered "onClick" event but I get a typecast error. Could anyone suggest how I could achieve this.

exception:

 Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.RadioButtonList' to type 'Club.WebControlButton'.

new webControl class:

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> Public Class WebControlButton
Inherits WebControl
Implements IPostBackEventHandler

' Define the Click event.
Public Event Click As EventHandler


' Invoke delegates registered with the Click event.
Protected Overridable Sub OnClick(ByVal e As EventArgs)
    RaiseEvent Click(Me, e)
End Sub


' Define the method of IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
Implements IPostBackEventHandler.RaisePostBackEvent

    OnClick(New EventArgs())
End Sub

End Class

 Private Sub AddOnClickStringsToElements()
    Dim divider As Integer = If(onClickElements.Count > 0, onClickElements.Count, 1)
    Dim percentIntervals As Integer = 100 / divider
    For Each element As Tuple(Of String, WebControl, Integer) In onClickElements
        If element.Item3 < 1 Then
            element.Item2.Attributes("onClick") = GetAnimateJavascript(percentIntervals)
        Else
            element.Item2.Attributes("onClick") = GetAnimateJavascript(element.Item3)
        End If
        //throws typecast exception here
        AddHandler CType(element.Item2, WebControlButton).Click, AddressOf UpdatePercent_OnClick 
    Next
End Sub

Answers


It looks like the onClickElements collection contains an actual RadioButtonList control, not a control which derives from your WebControlButton class.

Any control which you are putting into the onClickElements collection must derive from WebControlButton for that casting operation to work e.g.

Public Class MyRadioButtonList
    Inherits WebControlButton

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        '   Create your radio button list here..
    End Sub

End Class

An easier way would be to make an interface, such as IClickable...something a bit like this..sorry I didn't type it in VS so not sure if I got the syntax exact.

Public Interface IClickable
   Public Event OnClick()
End Interface

Public Class MyRadioButtonList
   Inherits RadioButtonList
   Implements IClickable

   Public Event OnClick Implements IClickable.OnClick

   Private Sub RaiseOnClickEvent Handles Me.OnSelectedIndexChanged
      RaiseEvent OnClick()
   End Sub

End Class

And then..

For Each element As Tuple(Of String, WebControl, Integer) In onClickElements
  If TypeOf(element.item2) Is IClickable Then
     AddHandler CType(element.Item2, IClickable).OnClick, AddressOf UpdatePercent_OnClick
  Else
    Throw New Exception("A control of type IClickable was expected")
  End If
Next

Need Your Help

MSSql (Compact) DELETE-Query with JOIN

sql left-join sql-server-ce sql-delete

I have this query. I want to delete all entities from AgentsResultLinks-Table, that don't have a link to a entity in Results-Table. I want a solution with one single query.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.