ASP.Net: User controls added to placeholder dynamically cannot retrieve values

前端 未结 8 1890
攒了一身酷
攒了一身酷 2021-02-09 02:53

I am adding some user controls dynamically to a PlaceHolder server control. My user control consists of some labels and some textbox controls.

When I submit the form

8条回答
  •  天命终不由人
    2021-02-09 03:34

    This is based on .NET v1 event sequence, but it should give you the idea:

    • Initialize (Init event)
    • Begin Tracking View State (checks if postback)
      • Load View State (if postback)
      • Load Postback Data (if postback)
    • Load (Load event)
      • Raise Changed Events (if postback)
      • Raise Postback Events (if postback)
    • PreRender (PreRender event)
    • Save View State
    • Render
    • Unload (Unload event)
    • Dispose

    As you can see, the loading of ViewState data back to the controls happen before the Load event. So in order for your dynamically-added controls to "retain" those values, they have to be present for the ASP.NET page to reload the values in the first place. You would have to re-create those controls at the Init stage, before Load View State occurs.

提交回复
热议问题