Results 1 to 8 of 8

Thread: Persisting ViewState on a dynamically created user control

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Persisting ViewState on a dynamically created user control

    I've got a user control that represents a Parameter for a report. It has controls to pick / type a value as well as a valuechanged event.
    I want its value to be persisted across postbacks.

    I've got a page which dynamically creates these controls based on the report the user wants to view.

    In order to get the report parameters i need to access the page's ViewState.

    From doing research it seems i need to create my dynamic controls before LoadViewState occurs, however if i try this obviously i don't have access to the viewstate in order to determine who many controls i should create.

    I've tried adding them in the Page_Load event, but have had no luck getting values back from the control's viewstate.

    Any help is very much appreciated.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Persisting ViewState on a dynamically created user control

    If you add/create dynamic controls in page_int the viewstate will be accessable in page_load. This is the general rule of thumb with controls added at runtime - it also makes the event handlers work like button click sub.

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Persisting ViewState on a dynamically created user control

    Thanks for the reply, but i need access to the viewstate in order to determine the number of controls to create so i can't create them in the page_init event.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Persisting ViewState on a dynamically created user control

    It seems the reason i can't create the controls in page_load is that the clientID is inconsistent. Even though i set the ID manually when loading the control it seems that some times the client id is the id of the control, other times it's a 'proper' client id, i.e. prefixed with IDs of parent controls (in this case the master page's content area).

    Any thoughts would be very much appreciated as i've pretty much hit a wall with this.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Persisting ViewState on a dynamically created user control

    Hello,

    Are you aware that there is a difference between the Server Side ID, and the ClientID. The reason for this is that a control may be rendered several times on the same page. For instance, a textbox called TxtBox1, which is rendered on each row of a GridView. As a result, it has to make the ClientID unique.

    Gary

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Persisting ViewState on a dynamically created user control

    I don't understand what the clientID has to do with server side code? Unfortuneately asp.net does not give full control over clientID but 4.0 will be introducing that feature for us.

    I think your in a catch 22 with viewstate needed before you add your controls and your controls needing to be added in page_int so they can take advantage of viewstate.

    I have not overriden the load or save viewstate events and I think it could be fraught with problems to add your own values that way. Me I'd probably just go for a cheap work around like stuffing those viewstate value somewhere else

  7. #7

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Persisting ViewState on a dynamically created user control

    I'm aware the ID and clientID are different.

    The clientID is used in serverside code for storeing/retreiving the viewstate (i believe).

    My solution for now is to store things in the session variable, but obviously this isn't that create since i've got to manage unique IDs myself (since i can't rely on clientID).
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


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

    Re: Persisting ViewState on a dynamically created user control

    Store the 'details' of the dynamic control created in a hidden field; read the hidden field to get the value out which could be, for example, the number of rows in your dynamic control. Read it when you need it.

Tags for this Thread

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