473,372 Members | 814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

WebControl Problem - Persistance? (Cross-Post)

Hello everyone! I'm having a problem with a WebControl that I'm designing
for a Menu. I've been at it for about 3 weeks now, and can't seem to get
around this problem. So I'm hoping that someone can help me ...

My environment: VS 2003 v7.1.3088, Win2K v5.0.2195 SP3, IE6 v6.0.2800.1106

I have a class (C3Menu) derived from WebControl, with a property (MenuItems)
that is a collection of menu items. The collection property is defined as a
class which inherits from CollectionBase. I also have a Designer associated
to the C3Menu class which allows design-time support.
See the code below for more details.

The control is generating properly - the menu items are being persisted to
the HTML view, and the control is rendering ok in both design view and at
run-time. An ellipse button appears next to the MenuItems collection, which
allows me to add / remove items from the collection via the standard
collection editor - everything seems normal!

The problem is when I use the following sequence of events:
1) I run my test application to bring the page up in IE6, everything looks
2) I close the page to return back to the VS editor.
3) I single-click my control to display the properties in the property
4) I click the ellipses button to edit the menuitems collection.
5) I click Add to add a new item to the collection - I see the item added in
the left pane of the collection editor.
6) I click Ok to return to VS view.
7) The item(s) I just added do not persist to the HTML view, nor to the
design view!

It gets better! If I click to the HTML view and back to the Design view,
the design view gets updated with the new items - but if I click back to the
HTML view, the items are still not persisted! If I go through the above
sequence of events again (immediately after the HTML/Design view switch), I
still see my new items I added - and when I add another item (via collection
editor) and select Ok to return to design view it updates it with ALL my new
items I added and the items are persisted in the HTML view!

It gets even weirder! I can go through the above sequence of events, but if
I select another web control between steps 2 and 3 (a Label for instance)
everything is persisted correctly.

I originally wrote my own custom collection editor and was having the same
problem, so I decided just to utilize the standard collectioneditor - with
the same results. After all of the reading I've done, I'm thinking it has
something to do with a TypeConverter but am not for sure. I added the type
converter on the MenuItem (collection item) definition, but it did not seem
to help.

Any ideas? I would be happy to send the code for anyone to look at, as this
is more of a learning experience for me than anything. This is my first
design-time control writing attempt - any help would be appreciated.


Todd Lucas
email: Todd.Lucas -at- cox.net

Code snippets:
C3Menu.vb - C3Menu Class Definition
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Web.UI
Imports System.Collections.Specialized
Imports System.Web.UI.WebControls.CommandEventArgs
Imports System.Web.UI.WebControls
Imports System.Drawing
Imports System.Drawing.Design

Namespace C3WebControls

<DefaultProperty("MenuItems"), DefaultEvent("ItemClick"), _
Description("Server Control - Menu Definition."), _
ParseChildren(True), PersistChildren(False), _
Designer(GetType(C3WebControls.C3Menu_Designer)), _
ToolboxData("<{0}:C3Menu runat=server></{0}:C3Menu>")> _
Public Class C3Menu : Inherits System.Web.UI.WebControls.WebControl
' Menu items storage:
Dim _menuitems As New C3MenuItems
<Bindable(False), Category("Menu Properties"), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty), _
Editor(GetType(C3WebControls.C3MenuItem_Collection Editor),
GetType(UITypeEditor)), _
Description("Collection of Menu Items this object will display.")> _
Public ReadOnly Property [MenuItems]() As C3MenuItems
Return _menuitems
End Get
'This property has no SET value!
End Property
C3MenuItems.vb - C3MenuItems Class Definition

Imports System.Web.UI
Imports System.Globalization
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization

Namespace C3WebControls

' Menu Item Collection Class:
<Description("Menu Item Collection Definition.")> _
Public Class C3MenuItems : Inherits CollectionBase

Default Public Property Item(ByVal index As Integer) As C3MenuItem
Return DirectCast(MyBase.List.Item(index), C3MenuItem)
End Get
Set(ByVal value As C3MenuItem)
MyBase.List.Item(index) = value
End Set
End Property

Public Overridable Function Add(ByVal value As C3MenuItem) As Integer
End Function

Public Overloads Sub Remove(ByVal Index As Integer)

' ensure that the object exists in the collection.
Dim mnuitm As C3MenuItem
mnuitm = CType(Me.InnerList.Item(Index), C3MenuItem)

' remove object to the collection (by index).
If Not mnuitm Is Nothing Then Me.InnerList.Remove(mnuitm)

End Sub

End Class

End Namespace
C3MenuItem.vb - C3MenuItem Class Definition

Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.CommandEventArgs
Imports System.Drawing.Design
Imports System.Drawing
Imports System.Reflection
Imports System.Globalization
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization

Namespace C3WebControls

<DefaultProperty("Text"), _
TypeConverter(GetType(C3MenuItem_TypeConverter)), _
Description("Menu Item Properties Definition.")> _
Public Class C3MenuItem

<NonSerialized()> _
Public pOwningMenu As C3Menu

' MenuItem property storage:
Dim _align As String = ""
Dim _closeonclick As Boolean = True
Dim _color3dhigh As Color = ColorTranslator.FromHtml("")

.... all properties are 1 of the above type - string, boolean, color.
.... standard Property Get / Set ...

Public Function Clone() As C3MenuItem

Dim BinFormatter As New
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter
Dim memStream As New System.IO.MemoryStream

' Serialize the memorystream object to this instance of the object.
BinFormatter.AssemblyFormat =
Runtime.Serialization.Formatters.FormatterAssembly Style.Simple
BinFormatter.Serialize(memStream, Me)
memStream.Position = 0

' DeSerialize the memorystream object.
Dim newItm As New C3MenuItem
newItm = CType(BinFormatter.Deserialize(memStream), C3MenuItem)

' return the clone.
Return newItm

End Function

End Class
Friend Class C3MenuItem_TypeConverter : Inherits TypeConverter

Public Overloads Overrides Function CanConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If sourceType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function

Public Overloads Overrides Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destType As Type) As Boolean
If destType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertTo(context, destType)
End Function

Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object,
ByVal destType As Type) As Object
If destType Is GetType(InstanceDescriptor) Then
Dim ci As ConstructorInfo =
GetType(C3MenuItem).GetConstructor(System.Type.Emp tyTypes)
Return New InstanceDescriptor(ci, Nothing, False)
End If
Return MyBase.ConvertTo(context, culture, value, destType)
End Function

End Class

C3Menu_Designer.vb - C3Menu Designer Definition

Imports System.Web.UI.Design
Imports System.ComponentModel.Design
Imports System.ComponentModel
Imports System.Drawing.Design

Namespace C3WebControls

' Designer class for the C3Menu control.
Public Class C3Menu_Designer : Inherits ControlDesigner

' Use PreFilterProperties to remove properties from the designer view.
Protected Overloads Overrides Sub PreFilterProperties(ByVal Properties As

' Invoke base class method:

' Remove specified properties from design view:

End Sub

' Returns the html to use to represent an empty control at design time.
Protected Overrides Function GetEmptyDesignTimeHtml() As String

Dim sTxt As String = "No Menu Items Defined"
Return CreatePlaceHolderDesignTimeHtml(sTxt)

End Function

' Returns the html which represents the control at design time.
Public Overrides Function GetDesignTimeHtml() As String

' Invoke base class method.
Dim html As String = MyBase.GetDesignTimeHtml()

' Set reference to current control instance.
Dim mnu As C3Menu = DirectCast(Component, C3Menu)

Dim tblstyle As String
tblstyle &= "width:" & mnu.Width.ToString & "; "
tblstyle &= "height:" & mnu.Height.ToString & "; "

' If no items defined then display this.
If mnu.MenuItems.Count = 0 Then Return html

Dim itm As C3MenuItem
Dim styl As C3MenuStyle
Dim itmstyle As String

' set reference to menustyle object.
styl = DirectCast(mnu.FindControl(mnu.MenuStyle), C3MenuStyle)

' if style was assigned, then use style font.
If Not styl Is Nothing Then
If Len(styl.FontFamily) > 0 Then tblstyle &= "font-family:" &
styl.FontFamily & "; "
If Len(styl.FontSize) > 0 Then tblstyle &= "font-size:" & styl.FontSize
& "; "
If Len(styl.FontWeight) > 0 Then tblstyle &= "font-weight:" &
styl.FontWeight & "; "
End If

' Build a table for menu items.
html = ""
html &= "<table style=" & Chr(34) & tblstyle & Chr(34) & ">" & vbCrLf
If LCase(mnu.Orientation) = "horizontal" Then html &= "<tr>"

' Display menu as defined.
For Each itm In mnu.MenuItems

If Not LCase(mnu.Orientation) = "horizontal" Then html &= "<tr>" &

html &= "<td>" & itm.Text & "</td>" & vbCrLf
If Not LCase(mnu.Orientation) = "horizontal" Then html &= "</tr>" &


If LCase(mnu.Orientation) = "horizontal" Then html &= "</tr>"
html &= "</table>"

'System.Windows.Forms.MessageBox.Show("GetDesignTi meHtml - HTML:" &
vbCrLf & vbCrLf & html, "C3Menu_Designer")
Return html

End Function

End Class

End Namespace
WebForm1.aspx - WebForm HTML View

<cc1:C3Menu id="C3Menu1" style="LEFT: 208px; TOP: 120px" runat="server"
<cc1:C3MenuItem Text="Item Text"></cc1:C3MenuItem>
<cc1:C3MenuItem Text="Item Text"></cc1:C3MenuItem>
<cc1:C3MenuItem Text="Item Text"></cc1:C3MenuItem>
<cc1:C3MenuItem Text="Item Text"></cc1:C3MenuItem>
<cc1:C3MenuItem Text="Last"></cc1:C3MenuItem>
Nov 18 '05 #1
0 1574

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Leo | last post by:
Hi, I'm back to ASP for a short while and I was wondering how I could save the entire Application/Session-state to a file or database and how to read it back afterwards. I'd like to preserve...
by: Michael.McD | last post by:
Is there any consensus on the way to go when implementing object persistance to a dB? For example MHibernate v. DataObjects (x-tensive). Cheers, Michael McD
by: Owatona | last post by:
Hi to all... I have 3 classes derived from webcontrol. I the first i have two dropdownlist, in the second i have some buttons, and the third its the webcontrol that are instanciate the two...
by: Matze | last post by:
I have a function that has an Argument of type System.Web.UI.WebControls.WebControl. I want to set the text-property of that control. Of course it doesnt work if I cast that control to...
by: kw | last post by:
What is the proper way to get the element ID for a client script? For example, suppose in the WebControl: TextBox t=new TextBox; t.ID=this.ClientID+"X"; .... Then elsewhere we want to access...
by: Novice | last post by:
Hey all, I've already posted this question and two posters offered suggestions and they worked - but now I would like to know why - and if possible the answer to a second question. Here is my...
by: anon | last post by:
Dear Microsoft: In your new ASP.NET 2.0 page model of being able to crosspost to another page, it would seem that if you were to type that word in Google, "crosspost" or "cross post", you will...
by: Dariusz Tomon | last post by:
Hi In my project I have got several webcontrol (ascx). How can I handle with the problem: I would like to have some ASPNET object (DropDownList, InputBox etc.) in one webcontrol (this...
by: Christian Hofmann | last post by:
Hello, How is it possible to use a hyperlink webcontrol inside a label webcontrol? I am using resources for globalisation. Now I have a text like this: Please click here to open that...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.