Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Persist custom object

Status
Not open for further replies.

fawkes

Technical User
Sep 12, 2003
343
GB
I'm new to asp.net and Im using Visual Web Developer Express Edition 2005 (asp.net 2.0)

I've written a class in vb.net using .net 2.0 and want to use it in a web control but whenever the page posts back the web control resets itself.

How do I get around this?
 
If it's a dynamically added user control, you will have to create it each time the page loads.

Also, if you want to persist data, have a look at adding the relevant information to ViewState.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Also, if you want to persist data, have a look at adding the relevant information to ViewState.

I'm new to asp.net, is there a web site that'll explain viewstate?
 
OK

Looked at the two websites, thanks for your help.

I now realise I need to understand viewstate and serialisation, what would be great is if there is a walkthrough anywhere out there.
 
Doing my head in now

Here's my custom control

Code:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Runtime.Serialization

''' <summary>
''' Delegate to control the WebDimensionInterface events
''' </summary>
''' <param name="sender"></param>
''' <remarks></remarks>
Public Delegate Sub WebControlEventDelegate( _
                    ByVal sender As EngineeringDimensions.WebDimensionInterface)


<DefaultProperty("Text"), ToolboxData("<{0}:WebDimensionInterface runat=server></{0}:WebDimensionInterface>")> _
<Serializable()> _
Public Class WebDimensionInterface
    Inherits WebControl

#Region " Events "

    Private m_SerializableHandlers As New Generic.List(Of WebControlEventDelegate)
    Private m_NonSerializableHandlers As New Generic.List(Of WebControlEventDelegate)

    Public Custom Event ControlValuesChanged As WebControlEventDelegate
        AddHandler(ByVal value As WebControlEventDelegate)
            If value.Target.GetType.IsSerializable Then
                Me.m_SerializableHandlers.Add(value)
            Else
                Me.m_NonSerializableHandlers.Add(value)
            End If
        End AddHandler

        RemoveHandler(ByVal value As WebControlEventDelegate)
            If value.Target.GetType.IsSerializable Then
                Me.m_SerializableHandlers.Remove(value)
            Else
                Me.m_NonSerializableHandlers.Remove(value)
            End If

        End RemoveHandler

        RaiseEvent(ByVal sender As WebDimensionInterface)

            For Each item As WebControlEventDelegate In Me.m_SerializableHandlers
                item.Invoke(sender)
            Next

            For Each item As WebControlEventDelegate In Me.m_NonSerializableHandlers
                item.Invoke(sender)
            Next

        End RaiseEvent
    End Event

#End Region 'Events 

#Region " Child Controls "

    Private WithEvents m_DimensionLabel As System.Web.UI.WebControls.Label
    Private WithEvents m_MagnitudeTextBox As System.Web.UI.WebControls.TextBox
    Private WithEvents m_UnitsComboBox As System.Web.UI.WebControls.DropDownList
    Private WithEvents m_DirectionTextBox As System.Web.UI.WebControls.TextBox
    Private WithEvents m_DirectionUnitsComboBox As System.Web.UI.WebControls.DropDownList

    Protected Overrides Sub CreateChildControls()

        'Add the child controls

        'Preset the text values
        Me.m_DimensionLabel.Text = Me.DimensionLabelText
        Me.m_MagnitudeTextBox.Text = "0"
        Me.m_DirectionTextBox.Text = "0"

        'Set the widths of the controls
        Me.m_DimensionLabel.Width = 100
        Me.m_MagnitudeTextBox.Width = 100
        Me.m_UnitsComboBox.Width = 100
        Me.m_DirectionTextBox.Width = 100
        Me.m_DirectionUnitsComboBox.Width = 100

        'Add the controls
        Me.Controls.Add(Me.m_DimensionLabel)
        Me.Controls.Add(Me.m_MagnitudeTextBox)
        Me.Controls.Add(Me.m_UnitsComboBox)
        Me.Controls.Add(Me.m_DirectionTextBox)
        Me.Controls.Add(Me.m_DirectionUnitsComboBox)

    End Sub

#End Region 'Child Controls 

#Region " Child Control Dimensions "

    Public Property LabelWidth() As System.Web.UI.WebControls.Unit
        Get
            Return Me.m_DimensionLabel.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            Me.m_DimensionLabel.Width = value
        End Set
    End Property

    Public Property MagnitudeWidth() As System.Web.UI.WebControls.Unit
        Get
            Return Me.m_MagnitudeTextBox.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            Me.m_MagnitudeTextBox.Width = value
        End Set
    End Property

    Public Property MagnitudeUnitsWidth() As System.Web.UI.WebControls.Unit
        Get
            Return Me.m_UnitsComboBox.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            Me.m_UnitsComboBox.Width = value
        End Set
    End Property

    Public Property DirectionWidth() As System.Web.UI.WebControls.Unit
        Get
            Return Me.m_DirectionTextBox.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            Me.m_DirectionTextBox.Width = value
        End Set
    End Property

    Public Property DirectionUnitsWidth() As System.Web.UI.WebControls.Unit
        Get
            Return Me.m_DirectionUnitsComboBox.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            Me.m_DirectionUnitsComboBox.Width = value
        End Set
    End Property


#End Region

#Region " Child Control Enabled "

    Public Property MagnitudeEnabled() As Boolean
        Get
            Return Me.m_MagnitudeTextBox.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.m_MagnitudeTextBox.Enabled = value
        End Set
    End Property

    Public Property MagitudeUnitsEnabled() As Boolean
        Get
            Return Me.m_UnitsComboBox.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.m_UnitsComboBox.Enabled = value
        End Set
    End Property

    Public Property DirectionEnabled() As Boolean
        Get
            Return Me.m_DirectionTextBox.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.m_DirectionTextBox.Enabled = value
        End Set
    End Property

    Public Property DirectionUnitsEnabled() As Boolean
        Get
            Return Me.m_DirectionUnitsComboBox.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.m_DirectionUnitsComboBox.Enabled = value
        End Set
    End Property

#End Region 'Child Control Enabled 

#Region " Public Properties "


    Private m_DimensionLabelText As String
    Public Property DimensionLabelText() As String
        Get
            Return Me.m_DimensionLabelText
        End Get
        Set(ByVal value As String)
            Me.m_DimensionLabelText = value
            If Not Me.m_DimensionLabel Is Nothing Then
                Me.m_DimensionLabel.Text = value
            End If
        End Set
    End Property


#End Region 'Public Properties 

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        EnsureChildControls()
        RenderChildren(output)
    End Sub

    Public Sub New()

        'Initialise the child controls
        Me.m_DimensionLabel = New System.Web.UI.WebControls.Label
        Me.m_MagnitudeTextBox = New System.Web.UI.WebControls.TextBox
        Me.m_UnitsComboBox = New System.Web.UI.WebControls.DropDownList
        Me.m_DirectionTextBox = New System.Web.UI.WebControls.TextBox
        Me.m_DirectionUnitsComboBox = New System.Web.UI.WebControls.DropDownList


    End Sub

#Region " Child Changes Events "

    Private Sub m_MagnitudeTextBox_TextChanged(ByVal sender As Object, _
                                                ByVal e As System.EventArgs) _
                                                    Handles m_MagnitudeTextBox.TextChanged
        RaiseEvent ControlValuesChanged(Me)

    End Sub

    Private Sub m_DirectionTextBox_TextChanged(ByVal sender As Object, _
                                                ByVal e As System.EventArgs) _
                                                    Handles m_DirectionTextBox.TextChanged
        RaiseEvent ControlValuesChanged(Me)

    End Sub

    Private Sub m_UnitsComboBox_SelectedIndexChanged(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles m_UnitsComboBox.SelectedIndexChanged

        'Raise the controls changed event
        RaiseEvent ControlValuesChanged(Me)

    End Sub

    Private Sub m_DirectionUnitsComboBox_SelectedIndexChanged(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles m_DirectionUnitsComboBox.SelectedIndexChanged

        RaiseEvent ControlValuesChanged(Me)

    End Sub

#End Region 'Child Changes Events 

End Class

In my default asp page I have the following code:

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then
            ViewState.Add("mytext", "randomtext")
            ViewState.Add(Me.WebDimensionInterface1.ID, Me.WebDimensionInterface1)
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Label1.Text = Me.ViewState("mytext")
    End Sub
End Class

The page has an instance of my custom control, a button and a label but I keep getting the message that the custom control is not marked as serializable.

But it is, and I've set the event handler as shown at
I can't see what else to do
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top