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

Discussion in 'ASP .Net Building Controls' started by Namshub, Jan 25, 2005.

  1. Namshub

    Namshub Guest

    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.
    Namshub, Jan 25, 2005
    #1
    1. Advertising

  2. Namshub

    Teemu Keiski Guest

    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.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU

    "Namshub" <> wrote in message
    news:...
    >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.
    >
    >
    >
    >
    Teemu Keiski, Jan 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,685
    sleigh
    Feb 12, 2004
  2. Sriram
    Replies:
    0
    Views:
    110
    Sriram
    Jun 2, 2004
  3. Chad
    Replies:
    0
    Views:
    219
  4. Sriram
    Replies:
    0
    Views:
    118
    Sriram
    Jun 1, 2004
  5. Bob Jones

    Composite control - sub control as a property not rendering

    Bob Jones, Apr 6, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    241
    Bob Jones
    Apr 9, 2007
Loading...

Share This Page