Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Change User Control in ASP.Net

I'm trying to create a web page that will display an appropriate user control based on the selected value of a drop down list.

Basically the page layout is this:

Drop Down Selection
< User Control created based on drop down selection >

I have it half working... the controls are changing when the selection changes. In OnInit(), I dynamically create the last selected control (whose value gets saved in session state because ViewState isn't available at OnInit).

When the drop down selection change occurs, I remove the old user control, and add a new one. The problem is: with the new control being added from the selection changed event, I'm not able to save changes from the user on the first postback. After the first post back, the selected control is created from OnInit instead of the Change event, and state is saved from then on, until the next selection change.

Here is the SelectionChanged method:

protected void SelectionChanged(object sender, EventArgs e)
{
    SelectedValue = int.Parse(DropDownList.SelectedValue);  //Store in Session
    Control userControl = GetSpecificUserControl(SelectedValue);
    PlaceHolder1.Controls.Clear();   // Remove old user control
    PlaceHolder1.Controls.Add(userControl);
}

Any changes made to the new control by the user after SelectionChanged happens are not saved on the following post back. However, subsequent postbacks do get saved. At that point, the control is getting created in OnInit().

Is there some way to force the correct post back and ViewState when the control changes? Is it possible to force a page reinitialization after the control is changed?

like image 359
grimus Avatar asked Jun 12 '09 16:06

grimus


3 Answers

What you need to do is keep the last known value of the DropDownList in the Session. Then:

OnInit:

  • Create whatever control is indicated by the saved value in the session

SelectionChanged Event

  • Remove whatever you created during OnInit
  • Create and add new control based on new DropDownList selection
  • Save new DropDownList selection in session

This way, on the next postback after a change you are re-creating the control that ViewState expected to find, and so it's state will be restored.

Dynamic controls can be very finicky. Often it is easier to create all of the controls you might possible need and set their Visible properties to false. This way they don't render to the browser at all. Then set Visible to true for just the controls you need when you need them.

like image 110
Joel Coehoorn Avatar answered Nov 01 '22 09:11

Joel Coehoorn


This is the classic tear-your-hair-out problem with ASP.Net webforms. You have several options:

1) This is a bit of a hack, since it goes outside the intended page lifecycle a bit, but in my experience it's the most direct way of dealing with the problem. When the page posts back from the drop down selection event, simply poll Request["MyDropDownID"] for the selected value of the drop down control during Init() - don't wait for the OnMyDropDownChanged() event to set up your page.

2) Implement your own ViewState handling for your user controls. This requires digging into the ViewState documentation and overriding a number of methods.

3) Joel's solution. He beat me to it but I was trying to get first post :p

Other options involve posting values using javascript and such, but those get really messy.

like image 43
womp Avatar answered Nov 01 '22 08:11

womp


If the list of options is not too big, you could just render all the user controls statically and use JavaScript/jQuery to show/hide the appropriate controls based on the value of the dropdown (onchange js event). You can use the dropdown value to extract the appropriate values from the user controls when saving.

You avoid the pain of dealing with dynamic controls, provide a more responsive UI when selecting from the dropdown (no postback), etc...

like image 22
HectorMac Avatar answered Nov 01 '22 08:11

HectorMac