Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Web Part Life Cycle and Processing Forms

  Asked By: Gonzalo    Date: Dec 21    Category: Sharepoint    Views: 6839

I have a webpart that does the following.

1. Retrieve a current record from a web service call
2. Provide a form to update that information
3. Save the record via web service.

The challenge is that one or more values in the record determine what
the form will look like. For example if the car make is Ford then I
would show a list of models for that make, which would need to be
retrieved via a web service.

So I have a listbox of "auto makes" with an event attached to
retrieve the models for that make on change.

The problem is that I am creating the form in CreateChildControls and
in the web part life cycle the post data is not updated until after
CreateChildControls has been done. Therefore I do not have the
change to the car make listbox when creating the page.

If I were to make ChildControlsCreated false then I can recreate the
controls, however any post data will be cleard from controls that I
add to the collection again.

Here is the solution that I came up with.

A) First request of part - ViewState["rec"] is not set
1) Get record from web service
2) Store record in viewstate
3) CreateChildControls based on ViewState

B) ChangeEvent Postback - ViewState["rec"] is set
1) CreateChildControls based on viewstate
2) Move Post data to ViewState
3) Process Event and store to viewstate
4) CreateChildControls based on viewstate

What do you think of this approach? Has anyone else come across the
same situation and what approach did you use?



1 Answer Found

Answer #1    Answered By: Dakota Shaffer     Answered On: Dec 21

I've used this type of solution  before as well.. seems to work well for me. I
don't see any apparent downside to following this method as long as you are not
storing too much stuff in the viewstate, or storing confidential information.

Didn't find what you were looking for? Find more on Web Part Life Cycle and Processing Forms Or get search suggestion and latest updates.