开发者

Sharepoint Web Part OnPreRender PostBack Dynamic TextBoxes Not Accessible In Button Handler

开发者 https://www.devze.com 2022-12-29 22:27 出处:网络
I have a custom webpart which extends System.Web.UI.WebControls.WebPart and implements an EditorPart.

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 ha开发者_运维百科s 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


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.


FWIW, You can store a values directly in the ViewState without having to access it via proxy inside a TextBox control. In this snippet, I wrapped access to the viewstate inside a plain old property:

public string WebServiceResult
{
  get
  {
    string text = (string) ViewState["WebServiceResult"];
    if (text != null)
       return text;
    else
       return string.Empty;
  }
  set
  {
    ViewState["WebServiceResult"] = value;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号