1

I have created an ASP.NET class derived from the standard WebControls.TextBox, with the intention of adding extra properties that will persist between post-backs. However, I cannot figure how to get the values in these properties to persist.

I have tried setting the value of the properties into the controls ViewState as part of the PreRender handler, but the value is then not accessible in the Init handler on the post-back, because the ViewState has not yet been setup.

I could look for the ViewState value in the Load handler of the control, but if the page/usercontrol that is using the control asks for the properties value during its Load handler, the control hasn't yet reached it's Load handler, and it therefore not there.

My current class looks something like this...

Public Class MyTextBox
    Inherits TextBox

    Private _myParam As String = ""
    Public Property MyParam As String
        Get
            Return _myParam
        End Get
        Set(value As String)
            _myParam = value
        End Set
    End Property

    Private Sub MyTextBox_Init(sender As Object, e As EventArgs) Handles Me.Init
        If Page.IsPostBack Then
            _myParam = ViewState("myParam")
        End If
    End Sub

    Private Sub MyTextBox_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
        ViewState("myParam") = _myParam
    End Sub
End Class

I must be missing something really simple, such as whether there is an attribute I can set against the property.

UPDATE

Thanks to @AVD pointing out that I really had very little clue about the ViewState and the Init / Load stages, I finally figured it all out.

If you have the time (and if you're doing any major ASP.NET work, you need to make the time) please read the Understand ASP.NET View State document that @AVD pointed me to. It will explain a lot.

However, what it didn't explain is if you place your control within a <asp:Repeater>, you may as well throw all the rules out of the window... and that is exactly the problem I was experiencing.

In the end, the way I managed to get it to work was to use a <asp:PlaceHolder> control within the repeater, create an instance of my control within the ItemDataBound handler of the repeater, and then add the control to the <asp:PlaceHolder>... all done within the Init section (which fortunately I'm able to do).

As Andrew found out in this previous question you can end up in a chicken/egg situation, where you need to create the controls in the Init, but you won't know what controls you need until the Load.

(I have still made AVD's answer the correct one, because in the context of my original question, it is absolutely correct).

Community
  • 1
  • 1
freefaller
  • 19,368
  • 7
  • 57
  • 87

1 Answers1

1

You have to store/retrieve value to/from ViewState within the properties accessors.

Public Property MyParam As String
    Get
       If IsNothing(ViewState("myParam")) Then
           return string.Empty
       End IF
       Return ViewState("myParam").ToString()
    End Get
    Set(value As String)
        ViewState("myParam") = value
    End Set
End Property
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Sorry, I can't get this to work - I still have the issue that the ViewState is not initialised at the point that the parent page/usercontrol requests the information (as part of it's `Load` handler) – freefaller Jun 25 '12 at 13:10
  • @freefaller Do not use _myParam and I don't understand why are you retrieving ViewState in **init** and assigning in **prerender** handler. – KV Prajapati Jun 25 '12 at 13:22
  • Because I was trying to get it to work. Unfortunately I am still hitting the same problem, no matter how I use your code. If the control is hosted on a page, and that page retrieves ctrl.MyParam as part of the pages `Load` handler, the value is still blank... because I believe the `ViewState` for the derived controls still hasn't been initialised (at least it always shows as `Nothing` as far as I can see) – freefaller Jun 25 '12 at 13:34
  • @freefaller I'd suggest to read this [MSDN doc - Init and load view stage](http://msdn.microsoft.com/en-us/library/ms972976.aspx) – KV Prajapati Jun 25 '12 at 13:40
  • I thought I had a handle on how this stuff worked... I'm currently reassessing that view! Wow, my head hurts. I'm now in a Catch22 situation where I need to create dynamic controls in the Init section, but using values that are only available in the page's Load section. Thanks for your help, and apologies for thinking I knew more than I obvious do – freefaller Jun 25 '12 at 14:26
  • 1
    I've finally got it all sorted, and the part that was missing from all the above (and a really apologise for missing this out, but I didn't think it was relevant) is that the control in question is held in a ``. After lots of research, it turns out that ASP.NET is really not very good at handling dynamicaly set variables on dynamic controls (whether derived or not) when contained within a repeater (unless those controls are added manually on bind). Apologies again – freefaller Jun 25 '12 at 16:14