Reputation: 19953
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).
Upvotes: 1
Views: 641
Reputation: 94645
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
Upvotes: 1