Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:46:40+00:00 2026-06-06T08:46:40+00:00

I have created an ASP.NET class derived from the standard WebControls.TextBox , with the

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:46:41+00:00Added an answer on June 6, 2026 at 8:46 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an ASP.NET class. In that class i would like to use
Good Morning, I have created an ASP.NET 3.5 webform that allows users to search
I have created a web page (ASP.NET) that includes a stylesheet to mimic Dynamics
In an asp.net webforms app I have created a base class for my pages
I have created a website in ASP.NET and have created a class and put
In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission
I have created my own Role Provider because I found the one that ASP.Net
I have created four ASP.NET Server controls that will inject a lot of JavaScript
I have created a class in my asp.net MVC 3 application's Model folder and
I have created a paginatedList and pager for that for my ASP.NET MVC project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.