Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Problems with Events from an Item Collection

Reply
Thread Tools

Problems with Events from an Item Collection

 
 
andres_giraldo
Guest
Posts: n/a
 
      05-31-2005
Hi!

I'm trying to build my own custom Menu webcontrol, but I'm having some
problems handling the events. I've got 3 classes: 1. A MenuItem Class (It
works fine, even independently), 2. A MenuItem Collection Class and 3. A Menu
Class that contains a MenuItem Collection Property.

The MenuItem supports Click events on the server side, but when I'm trying
to assign the Click event of an Item to the main Click event of the Menu, the
MenuItem's Click event are lost.

How can I solve this? I would really appreciate any suggestion, here's my
code, thanks a lot.






'**************** 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.RaisePostBackE vent
'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.RaisePostBackE vent
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";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Bug with nested repeaters. Item events are called twice per item =?Utf-8?B?SmFtZXMgR2V1cnRz?= ASP .Net 4 03-28-2007 01:46 AM
WebControl - CollectionEditor Problem. Changing id property of new added collection item causes not adding item to collection - Sergio ASP .Net Web Controls 0 05-29-2006 06:20 AM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
Problems with Events from Items Collection andres_giraldo ASP .Net Web Controls 0 05-31-2005 10:36 PM



Advertisments