Search code examples
c#asp.netviewstatecomposite-controlschildcontrol

Recreate child controls and ViewState


Lets explain the problem with a simple case:

Lets CC be a composite control.

CC has a variable called filter (string) stored in viewState.

CC has 2 static child controls:

  • searchBox: Textbox
  • searchButton: Button

The user type a search term and click on the searchButton.

the following steps happens:

  • PageLoad

  • CreateChildControls:

    • Create static child controls
    • Request database using filter
    • Create table with results and insert on each row an editButton (Button) and binding handler to the editButtons click event.
    • Insert table in childControls hierarchy.
  • Click event is fired

searchButton_click handler does the following:

  • update the filter
  • Recreate child controls this is where I am stucked

I need to recreate the child control hierarchy but also to maintain the child controls viewstate ! Otherwise, my editButtons's Click events will not be fired at all.

Is there a way to achieve this ? I keep searching but I cannot find any solution. I will be glad to get some help from you guys.

Hope you anderstand my problem, ask questions/code illustration if needed.

Best regards

Quick


Solution

  • Ok, i have found a solution:

    The trick was to give an ID to each editButton (string.format("editButton_{0}", i).

    If you have better solution, please let me know.