Results 1 to 6 of 6

Thread: Making web control values persist [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Making web control values persist [Resolved]

    I just made the world's simplest Web Control.

    VB Code:
    1. Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
    2.             Output.Write("<b><u>" & _writewhat & "</u></b>")
    3.         End Sub
    4.  
    5.         Public Property WriteWhat() As String
    6.             Get
    7.                 Return _writewhat
    8.             End Get
    9.             Set(ByVal Value As String)
    10.                 _writewhat = Value
    11.             End Set
    12.         End Property

    See?

    But I have no idea how I can make the values of these persist on postbacks.

    Either there's some code I have to write, or I'm overlooking something simple.

    So, how?
    Last edited by mendhak; Jul 20th, 2004 at 07:20 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Private _writewhat As String = "default if not assigned"
    2.  
    3. Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
    4.             Output.Write("<b><u>" & [b]Me.WriteWhat[/b] & "</u></b>")
    5.         End Sub
    6.  
    7.         Public Property WriteWhat() As String
    8.             Get
    9.                 [b]If Me.ViewState("writewhat") Is Nothing Then[/b]
    10.                     Return _writewhat
    11.                 [b]Else
    12.                     Return Me.ViewState("writewhat")
    13.                 End If[/b]
    14.             End Get
    15.             Set(ByVal Value As String)
    16.                 _writewhat = Value
    17.                 [b]Me.ViewState.Add("writewhat",Value)[/b]
    18.             End Set
    19.         End Property
    Last edited by nemaroller; Jul 20th, 2004 at 06:56 AM.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK... I've understood that, thanks.

    One more question, though:

    You're doing a check like this:

    VB Code:
    1. If Me.ViewState("writewhat") Is Nothing Then

    What exactly is this doing?

    Is it not possible to just do a:

    VB Code:
    1. Public Property WriteWhat() As String
    2.             Get
    3.                 Return CType(ViewState("WriteWhat"), String)
    4.  
    5.             End Get
    6.             Set(ByVal Value As String)
    7.                 ViewState("WriteWhat") = Value
    8.             End Set
    9.         End Property

    ?

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    No, wait, I did understand that line, my question is simply, can I do it the second way I just posted?

    No... ok, never mind. I understood.

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yea, you could do that.... doesn't matter...

    I only wonder how expensive a Ctype operation is... so even though it looks cleaner, you should at least try invoking Ctype only after you have checked to see there is a reason to...
    VB Code:
    1. Public Property WriteWhat() As String
    2.             Get
    3.                 If Me.ViewState("writewhat") Is Nothing Then
    4.                     Return _writewhat
    5.                 Else
    6.                     Return Ctype(Me.ViewState("writewhat"),String)
    7.                 End If
    8.             End Get

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    For that matter, ViewState seems to be pretty expensive too. So why not go for the whole shebang

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width