Problems with Events from Items Collection

A

andres_giraldo

Hi!
I'm building a webcontrol that is a Custom Menu that consists of three
classes: 1. A MenuItem, 2. A MenuItem Collection and 3. A Menu.

Now, the MenuItem class works fine, but when I've tried to assign an event
from a MenuItem of the MenuItem Collection in the Menu Class, it doesn't
works, the events of the MenuItem's are not called.

How can I solve this? Thanks!

Her'es my code, I really appreciate any suggestion, thanks for any advice!



'**************** Menu.vb


Imports System.ComponentModel

Public Enum Orientations
Horizontal = 0
Vertical = 1
End Enum

<DefaultEvent("Click")> _
Public Class Menu
Inherits WebControl

Implements IPostBackEventHandler

Public Event Click As EventHandler

Private _items As MenuItemCollection = New MenuItemCollection

<DefaultValue(0)> _
Public Property Spacing() As Integer
Get
Return CType(ViewState("_Spacing"), Integer)
End Get
Set(ByVal Value As Integer)
ViewState("_Spacing") = Value
End Set
End Property

<PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property Items() As MenuItemCollection
Get
Return _items
End Get
End Property

<DefaultValue(Orientations.Vertical)> _
Public Property Orientation() As Orientations
Get
Return CType(ViewState("_Orientation"), Orientations)
End Get
Set(ByVal Value As Orientations)
ViewState("_Orientation") = Value
End Set
End Property

Public Sub RaisePostBackEvent(ByVal eventArgument As String)
Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
'OnClick(Me, New EventArgs)
End Sub

Private Sub Menu_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Attributes("onClick") = Attributes("onClick") & ";" &
Page.GetPostBackEventReference(Me)
End Sub

Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)
Dim i As Integer

writer.Write("<TABLE BORDER='0' CELLPADDING='")
writer.Write(Spacing)
writer.Write("' CELLSPACING='")
writer.Write(Spacing)
writer.Write("'><TR>")

For i = 0 To Items.Count - 1
If (Orientation = Orientations.Vertical) Then
Items(i).Width = Me.Width
Else
Items(i).Height = Me.Height
End If

Controls.Add(Items.Item(i))

If (Orientation = Orientations.Vertical) Then
writer.Write("<TR>")
End If

writer.Write("<TD>")

Items.Item(i).RenderControl(writer)

writer.Write("</TD>")

If (Orientation = Orientations.Vertical) Then
writer.Write("</TR>")
End If
Next

writer.Write("</TR>")
writer.Write("</TABLE>")
End Sub

Protected Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent Click(sender, e)
End Sub

Private Sub Menu_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Init
Dim i As Integer

For i = 0 To Items.Count - 1
Items(i).AutoPostBack = True

AddHandler Items(i).Click, AddressOf OnClick
Next
End Sub
End Class



'**************** MenuItemCollection.vb


Public Class MenuItemCollection
Inherits CollectionBase

Default Public Property Item(ByVal index As Integer) As MenuItem
Get
Return CType(List(index), MenuItem)
End Get
Set(ByVal Value As MenuItem)
List(index) = Value
End Set
End Property

Public Function Add(ByVal value As MenuItem) As Integer
Return List.Add(value)
End Function 'Add

Public Function IndexOf(ByVal value As MenuItem) As Integer
Return List.IndexOf(value)
End Function 'IndexOf

Public Sub Insert(ByVal index As Integer, ByVal value As MenuItem)
List.Insert(index, value)
End Sub 'Insert

Public Sub Remove(ByVal value As MenuItem)
List.Remove(value)
End Sub 'Remove

Public Function Contains(ByVal value As MenuItem) As Boolean
' If value is not of type MenuItem, this will return false.
Return List.Contains(value)
End Function

Protected Overrides Sub OnInsert(ByVal index As Integer, ByVal value
As [Object])
If Not (TypeOf value Is MenuItem) Then
Throw New ArgumentException("value must be of type
MenuItem.", "value")
End If
End Sub

Protected Overrides Sub OnRemove(ByVal index As Integer, ByVal value
As [Object])
If Not (TypeOf value Is MenuItem) Then
Throw New ArgumentException("value must be of type
MenuItem.", "value")
End If
End Sub

Protected Overrides Sub OnSet(ByVal index As Integer, ByVal oldValue
As [Object], ByVal newValue As [Object])
If Not (TypeOf newValue Is MenuItem) Then
Throw New ArgumentException("newValue must be of type
MenuItem.", "newValue")
End If
End Sub

Protected Overrides Sub OnValidate(ByVal value As [Object])
If Not (TypeOf value Is MenuItem) Then
Throw New ArgumentException("value must be of type MenuItem.")
End If
End Sub
End Class




'**************** MenuItem.vb

Imports System.ComponentModel

<DefaultEvent("Click")> _
Public Class MenuItem
Inherits WebControl

Implements IPostBackEventHandler

Public Event Click As EventHandler

<DefaultValue(False)> _
Public Property AutoPostBack() As Boolean
Get
If (CType(ViewState("_autoPostBack"), Object) Is Nothing) Then
ViewState("_autoPostBack") = False
End If

Return CType(ViewState("_autoPostBack"), Boolean)
End Get
Set(ByVal Value As Boolean)
ViewState("_autoPostBack") = Value
End Set
End Property

Public Property Text() As String
Get
If (CType(ViewState("_Text"), Object) Is Nothing) Then
ViewState("_Text") = String.Empty
End If

Return CType(ViewState("_Text"), String)
End Get
Set(ByVal Value As String)
ViewState("_Text") = Value
End Set
End Property

Public Sub New()
InitVars("MenuItem")
End Sub

Public Sub New(ByVal itemText As String)
InitVars(itemText)
End Sub

Public Sub New(ByVal itemText As String, _
ByVal itemHeight As Integer, _
ByVal itemWidth As Integer)
InitVars(itemText, itemHeight, itemWidth)
End Sub

Public Sub RaisePostBackEvent(ByVal eventArgument As String)
Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnClick(New EventArgs)
End Sub

Public Overrides Sub RenderEndTag(ByVal writer As
System.Web.UI.HtmlTextWriter)
writer.Write(Text)

MyBase.RenderEndTag(writer)
End Sub

Private Sub InitVars( _
ByVal itemText As String, _
Optional ByVal itemHeight As Integer = 15, _
Optional ByVal itemWidth As Integer = 70)
BackColor = Color.FromArgb(255, 241, 241, 241)
Font.Name = "MS Sans Serif"
Font.Size = FontUnit.Parse("10")

Text = itemText
End Sub

Protected Overridable Sub OnClick(ByVal e As EventArgs)
RaiseEvent Click(Me, e)
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim script As New System.Text.StringBuilder

script.Append("<script language=javascript ")
script.Append("src='Menu.js'></script>")

Page.RegisterStartupScript("MenuItem", script.ToString)

Attributes("onMouseDown") = Attributes("onMouseDown") & _
";" & _
"chItem(this, " & _
"'default', " & _
"'#999999', '#999999')"

Attributes("onMouseOver") = Attributes("onMouseOver") & _
";" & _
"chItem(this, 'hand', " & _
"'#919191', '#f1f1f1')"

Attributes("onMouseOut") = Attributes("onMouseOut") & _
";" & "chItem(this, " & _
"'default', " & _
"'#f1f1f1', '#f1f1f1')"

Style.Add("border", "#f1f1f1 1px solid")

If (AutoPostBack) Then
Attributes("onClick") = Attributes("onClick") & ";" &
Page.GetPostBackEventReference(Me)
End If

MyBase.OnLoad(e)
End Sub
End Class


'********** Menu.js
function chItem(obj, cursor, borderColor, backgroundColor) {
obj.style.cursor = cursor;
obj.style.backgroundColor = backgroundColor;
obj.style.border = borderColor + " 1px solid";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,026
Latest member
TishaMadri

Latest Threads

Top