0

I have a page, where I want to dynamically add asp.net user controls. The scenario is that we want that on specific event of a control, It disposes itself and loads another control in the page.

I am not able to have solution about how to do this?

Anyone have decent idea?

4

2 に答える 2

0

このシナリオを想定してみましょう:

  • ショッピングカートページがあります。
  • 出荷管理が読み込まれます。
  • ユーザーは次のボタンをクリックします。

--- ポストバック ---

  1. ショッピングカートページが読み込まれました
  2. 出荷管理が読み込まれます。
  3. イベント「クリック」は出荷管理によって処理されます。
  4. 出荷管理は破棄されます。
  5. 支払管理がロードされます。

Session インスタンスに変数を格納して、どのコントロールをロードする必要があるかを判断できます。

ショッピング カート ページの PageInit で、Session 変数を取得し、対応するコントロールをロードします (ステップ 1 と 2)。ASP.NET がイベントを発生させるために、PageInit でこれを行うようにしてください。

次に、出荷管理のイベント ハンドラーで、Session 変数を更新します (ステップ 3)。

ショッピング カート ページの Page_LoadComplete で、出荷コントロールを破棄し (ステップ 4)、支払いコントロールを読み込みます (ステップ 5)。

Scott Mitchell は、このシナリオについて素晴らしい記事を書いています: http://scottonwriting.net/sowBlog/posts/3962.aspx

于 2009-10-27T16:00:37.073 に答える
0

Webforms or ASP.NET MVC? I'll assume webforms...

Try using a CompositeControl. If there is databinding involved you can use the DataBoundCompositeControl. In the CreateCHildren method you dynamically create your controls add add them to the child collection. Here is an example of a fairly complicated DataBoundCompositeControl I created once (with event handling on inner child controls):

Scaffolding Control

This is actually a really hard thing to get right. Just remember to rebuild all of your child controls everytime and to store the state of the control so you can recreate everything properly.

You will rebuild everything twice on postbacks (and once on the first GET). Once to recreate the controls to their previous state and the second time to process the changes after databinding and event handling.

于 2008-12-22T06:28:58.333 に答える