Dynamically added controls in Asp.Net

2020-01-27 19:44发布

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 with it. It might very well be because I'm trying to force the framework into something it is not intended for - so I'd like to learn how to do it "the right way". :-)

My problem is how to add controls to a page programmatically at runtime. As far as I can figure out you need to create the controls at page_init as they otherwise disappears at the next PostBack. But many times I'm facing the problem that I don't know which controls to add in page_init as it is dependent on values from at previous PostBack.

A simple scenario could be a form with a dropdown control added in the designer. The dropdown is set to AutoPostBack. When the PostBack occur I need to render one or more controls denepending on the selected value from the dropdown control and preferably have those controls act as if they had been added by the design (as in "when posted back, behave "properly").

Am I going down the wrong path here?

9条回答
神经病院院长
2楼-- · 2020-01-27 20:14

I think the answer here is in the MultiView control, so that for example the dropdown switches between different views in the multi-view.

You can probably even data-bind the current view property of the multiview to the value of the dropdown!

查看更多
霸刀☆藐视天下
3楼-- · 2020-01-27 20:15

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

查看更多
戒情不戒烟
4楼-- · 2020-01-27 20:21

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.

查看更多
登录 后发表回答