Property Not saved in Composite Control... Help & Pointers required.

N

Namshub

I have created a coposite control, consisting of text, labels , radiobuttons
and checkboxes the purpose is to create a generic control to manage user
accounts both as an admin and as a user. I have a property that tells me
wether they are adding a new user or modifying a current one.

a snippet is bellow:

Public Class UserAccount
Inherits UserControl
....
Private _DataMode As DataModeType
....
Public Property DataMode() As DataModeType
Get
Return _DataMode
End Get
Set(ByVal Value As DataModeType)
_DataMode = Value
End Set
End Property

Firstly how do i reference the user control on the form, once i've dragged
it onto the page?
I added manually the following is this correct?
Protected WithEvents useraccount1 As CWT2005.UserAccount

Now this does give me access to the comoponents, properties and methods, as
i've managed to set the property (via running the debugger), but when i call
the save method. (using a new button, sets the visiblity to true, and sets
the property to "New" (datamodetype))

UserAccount1.SaveValues()

and check the useraccount.datamode its defaulted back to "Edit"
(DatamodeType)

Can someone tell me what i'm doing wrong? The Useraccount1 control has
enabledviewstate = true. But do i have to include within the property code
adding and reading from viewstate? As i thought all properties would do
this automatically.

Thanks.
 
T

Teemu Keiski

Hi,

so that the proeprty value would be saved over postbacks, property should
use UserControl's ViewState collection as storage. Like this:

Public Property DataMode() As DataModeType
Get
If ViewState("DataMode") Is Nothing Then
Return DataModeType.<PutDefaultValueHere>
Else
Return CType(ViewState("DataMode"),DataModeType)
End If
End Get
Set(ByVal Value As DataModeType)
ViewState("DataMode") = Value
End Set
End Property

And to answer your question, no properties do not use ViewState
automatically unless they have been developed to do so. MS has built this
into premade controls (they also use ViewState but internally) but for your
own controls, you need to do this manually.

Yes, control declaration is correct. It works similarly as you see it
working with built-in controls and their declarations.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top