views:

365

answers:

2

I have a custom webpart which extends System.Web.UI.WebControls.WebPart and implements an EditorPart.

I have all the static controls being added in the overridden CreateChildControls method as I know this makes them persistent across PostBacks.

However, I have some TextBoxes being added in the overridden OnPreRender method because the actual TextBoxes that I add depend upon the data returned from a Web Service which I am calling in OnPreRender. The Web Service has to be called in OnPreRender because I need some of the Property values that are set in the EditorPart. If I build this logic into the CreateChildControls method, obviously the data is not available on first PostBack after an edit is applied because the PostBack events are restored after CreateChildControls. This means the page has to be posted twice before the data is refreshed and that is just cludgy.

How can make these TextBoxes along with their entered text values persistent across PostBacks so that I can access them in the button handler?

Thanks for any help, Matt

A: 

This ended up working for me:

  • Add a "hidden" TextBox in CreateChildControls (I achieved this with width=0; visible=false and enabled=false don't work)
  • In PreRender, check for any values entered in dynamic TextBox(es) for passing params to data call (Web Service in my case), this will come from parsing the value in the "hidden" TextBox (explained later)
  • After data call, add TextBox(es) ensuring to repopulate any previous values entered before last postback (by parsing "hidden" TextBox Text)
  • Finally, add an attribute to the button named "onClick" and set the value to a javascript function that acquires the values of the TextBoxes (using document.getElementById and passing TextBox.ClientID for each TextBox) and creates a formatted string to store the TextBox ID and value in the "hidden" TextBox.

Because the "hidden" TextBox is added in CreateChildControls, its value is restored and can be accessed on PostBack.

Hopefully this helps someone out there, I know I burned some time on this one.

Matt