Inheritance of web controls

PrOpHeT
03-13-2007, 03:12 PM
Is there some rule that you cannot inherit web controls?

in the following sample extended class...


Public Class TreeNodeEx
Inherits System.Web.UI.WebControls.TreeNode

#Region " Variables "
Private _Tag As Integer
#End Region

#Region " Properties "
Public Property Tag() As Integer
Get
Return _Tag
End Get
Set(ByVal value As Integer)
_Tag = value
End Set
End Property
#End Region
End Class


I have attempted to put a tag property on a WebControls.TreeNode because it does not have one. This method works well in winform applications (I have a very large treenodeex in one application). However the code appearing to be syntactically correct will allow me to add new treenodeex class instances to a treeview, but when enumerating the resulting treeview it tells me it cannot convert from type System.Web.UI.WebControls.TreeNode to treenodeex when I try to ctype the instances back out so I may get at their properties.


can you not inherit and then extend web control items?

wayneph
03-16-2007, 07:29 AM
Yes. You should be able to extend web controls using inherits. It's just like any other class.

I haven't worked much with the Trees in ASP.NET. Is there a chance that you are only using your class part of the time, and part of the time, it's a regular TreeNode? Like you only used your class on Leaf Nodes? I know I'm streatching here, but I can't think of any reason why it would be causing this problem.

PrOpHeT
03-16-2007, 09:29 AM
I just went back and ran a sample


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Node1 As New TreeNodeEx
Dim Node2 As New TreeNodeEx
Dim Node3 As New TreeNodeEx

Node1.Tag = 1
TreeView1.Nodes.Add(Node1)
Node2.Tag = 2
TreeView1.Nodes.Add(Node2)
Node3.Tag = 3
TreeView1.Nodes.Add(Node3)

For Each Item As TreeNodeEx In TreeView1.Nodes
Debug.WriteLine(Item.Tag)
Next

End Sub


and it worked, I had already deleted the other code that was erring so I guess I will go back and try it again, If it breaks again I am sure I will be back...

Thanks

wayneph
03-16-2007, 01:13 PM
Make sure you test it before an after a postback. Maybe something is being lost when the control is recreated on the server. The tag property may not be serialized into the ViewState correctly.

Just another thought.

PrOpHeT
03-17-2007, 04:37 PM
Hmm, looks like you hit the nail on the head the following code breaks


Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Node1 As New TreeNodeEx
Dim Node2 As New TreeNodeEx
Dim Node3 As New TreeNodeEx

Node1.Tag = 1
TreeView1.Nodes.Add(Node1)
Node2.Tag = 2
TreeView1.Nodes.Add(Node2)
Node3.Tag = 3
TreeView1.Nodes.Add(Node3)



End Sub

Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As TreeNodeEx In TreeView1.Nodes
Debug.WriteLine(Item.Tag)
Next
End Sub
End Class

Public Class TreeNodeEx
Inherits System.Web.UI.WebControls.TreeNode
Private _Tag As Integer

Public Property Tag() As Integer
Get
Return _Tag
End Get
Set(ByVal value As Integer)
_Tag = value
End Set
End Property
End Class


so how do you control how the system serializes the object to ensure the extended attributes are serialized as well?

(It is definitely the view state causing it, however if I disable the view state then I loose my selections in the tree. (it has checkboxes) :mad: )

shaul_ahuva
03-19-2007, 08:26 AM
I don't think it's the values that are/are not getting serialized into viewstate that are causing a problem, but what's happening on the re-creation of the control tree on postback.

For example, on the original post you add a bunch of TreeNodeEx instances to your TreeView. Since TreeNodeEx inherits TreeNode, everything works okay. If you're serializing your extended properties into view state (Control.ViewState, LoadViewState, SaveViewState, LoadControlState, or SaveControlState), the values get serialized correctly.

On the postback, however, the TreeView automatically re-creates the control structure without guidance from you. Unless it checks the type serialized into viewstate (if possible), it only knows about the TreeNode type and not the TreeNodeEx type. So, it re-creates the tree with TreeNode instances instead of TreeNodeEx instances.

In order to get TreeNodeEx instances on the postback, you'll need to extend the TreeView class and override the CreateNode method to return a TreeNodeEx instance.

PrOpHeT
03-19-2007, 10:20 AM
Ahhh, now that makes sense. I had never tried this in asp.net, just windows applications where it worked fine. Obviously asp.net is a different world. I will give it a shot, thanks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum