Dynamically added controls in Asp.Net

后端 未结 9 1570
情书的邮戳
情书的邮戳 2020-12-01 02:22

I\'m trying to wrap my head around asp.net. I have a background as a long time php developer, but I\'m now facing the task of learning asp.net and I\'m having some trouble w

相关标签:
9条回答
  • 2020-12-01 02:48

    After having wrestled with this problem for at while I have come up with these groundrules which seems to work, but YMMV.

    • Use declarative controls whenever possible
    • Use databinding where possible
    • Understand how ViewState works
    • The Visibilty property can go a long way
    • If you must use add controls in an event handler use Aydsman's tip and recreate the controls in an overridden LoadViewState.

    TRULY Understanding ViewState is a must-read.

    Understanding Dynamic Controls By Example shows some techniques on how to use databinding instead of dynamic controls.

    TRULY Understanding Dynamic Controls also clarifies techniques which can be used to avoid dynamic controls.

    Hope this helps others with same problems.

    0 讨论(0)
  • 2020-12-01 02:49

    The only correct answer was given by Aydsman. LoadViewState is the only place to add dynamic controls where their viewstate values will be restored when recreated and you can access the viewstate in order to determine which controls to add.

    0 讨论(0)
  • 2020-12-01 02:52

    Well. If you can get out of creating controls dynamicly, then do so - otherwise, what i whould do is to use Page_Load instead of Page_Init, but instead of placing stuff inside the If Not IsPostBack, then set i just directly in the method.

    0 讨论(0)
  • 2020-12-01 02:57

    If you truly need to use dynamic controls, the following should work:

    • In OnInit, recreate the exact same control hierarchy that was on the page when the previous request was fulfilled. (If this isn't the initial request, of course)
    • After OnInit, the framework will load the viewstate from the previous request and all your controls should be in a stable state now.
    • In OnLoad, remove the controls that are not required and add the necessary ones. You will also have to somehow save the current control tree at this point, to be used in the first step during the following request. You could use a session variable that dictates how the dynamic control tree was created. I even stored the whole Controls collection in the session once (put aside your pitchforks, it was just for a demo).

    Re-adding the "stale" controls that you will not need and will be removed at OnLoad anyway seems a bit quirky, but Asp.Net was not really designed with dynamic control creation in mind. If the exact same control hierarchy is not preserved during viewstate loading, all kinds of hard-to find bugs begin lurking in the page, because states of older controls are loaded into newly added ones.

    Read up on Asp.Net page life cycle and especially on how the viewstate works and it will become clear.

    Edit: This is a very good article about how viewstate behaves and what you should consider while dealing with dynamic controls: http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx

    0 讨论(0)
  • 2020-12-01 02:59

    You must add your control inside OnInit event and viewstate will be preserved. Don't use if(ispostback), because controls must be added every time, event in postback!
    (De)Serialization of viewstate happens after OnInit and before OnLoad, so your viewstate persistence provider will see dynamically added controls if they are added in OnInit.

    But in scenario you're describing, probably multiview or simple hide/show (visible property) will be better solution.
    It's because in OnInit event, when you must read dropdown and add new controls, viewstate isn't read (deserialized) yet and you don't know what did user choose! (you can do request.form(), but that feels kinda wrong)

    0 讨论(0)
  • 2020-12-01 03:00

    I ran across this in the book "Pro ASP.NET 3.5 in C# 2008" under the section Dynamic Control Creation:

    If you need to re-create a control multiple times, you should perform the control creation in the Page.Load event handler. This has the additional benefit of allowing you to use view state with your dynamic control. Even though view state is normally restored before the Page.Load event, if you create a control in the handler for the Page.Load event, ASP.NET will apply any view state information that it has after the Page.Load event handler ends. This process is automatic.

    I have not tested this, but you might look into it.

    0 讨论(0)
提交回复
热议问题