- - PR -
コレクション変数に代入したコントロールの動作について
1
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2004-05-21 14:33
はじめまして。『おばい』と申します。
最近、ASP.NET + VB.NETを勉強し始めました。 複数のImageButtonコントロールをWebフォーム上に動的に定義しよう考えています。 まず、ImageButtonコントロールをコレクション変数に割り当てて、コントロールを配列にしました。 下記プログラムを作成して、表示が出るところまでこぎつけましたのですが、ImageButtonコントロールをクリックしても、イベントハンドラ(ImageButton_Command)に処理が渡りません。 色々記事を探してみたのですが、分かりませんでした。 皆さんのお力をお借りしたく、お願いします。 ------------------------------------------------------------- Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton Protected WithEvents ImageButton2 As System.Web.UI.WebControls.ImageButton Protected WithEvents ImageButton3 As System.Web.UI.WebControls.ImageButton Protected WithEvents PlaceHolder As System.Web.UI.WebControls.PlaceHolder Public ImageCol As New Collection Private Sub Page_Load( ・・・略・・・ ) Handles MyBase.Load ImageButton1 = New System.Web.UI.WebControls.ImageButton ImageButton2 = New System.Web.UI.WebControls.ImageButton ImageButton3 = New System.Web.UI.WebControls.ImageButton ImageCol.Add(ImageButton1) ImageCol.Add(ImageButton2) ImageCol.Add(ImageButton3) PlaceHolder.Controls.Add(ImageCol(1)) End Sub Private Sub ImageButton_Command( ・・・略・・・ ) Handles _ ImageButton1.Command, _ ImageButton2.Command, _ ImageButton3.Command ・ ・ ・ End Sub | ||||
|
投稿日時: 2004-05-21 15:38
Page_Load()の中でImageButtonを毎回Newしていますよね。
クライアントの画面に表示されたImageButton1〜3と、その後サーバー側に処理が移った時にVB.NETのコードの中で見ているImageButton1〜3は別のオブジェクトです。 ですのでクリックされてもImageButton_Command()は呼び出されません。 かといって、AddHandler等を使ってもだめだと思います。 サーバー側へ処理が移ったときには既にクライアントの画面に表示されたImageButton1〜3は存在しないからです。(aspxにImageButton1〜3のタグがないから) | ||||
|
投稿日時: 2004-05-21 15:53
ご回答ありがとうございます。
-<引用>------------------------------------------------------- サーバー側へ処理が移ったときには既にクライアントの画面に表示された ImageButton1〜3は存在しないからです。 (aspxにImageButton1〜3のタグがないから) -------------------------------------------------------------- 理由につきましては何となくですが、理解しました。 と、言うことは、動的にコントロールを貼り付けた場合、そのコントロールのイベント により、イベントハンドラに処理が移せないと言うことでしょうか? お手数ですが、今一度ご教授下さい。 | ||||
|
投稿日時: 2004-05-21 16:05
こんにちは。
ここは参考になりますか。 | ||||
|
投稿日時: 2004-05-21 17:02
ありがとうございます。
イベントハンドラに処理が移りました。 -<引用>------------------------------------------------------- ここは参考になりますか。 http://support.microsoft.com/default.aspx?scid=kb;ja;317515#3 -------------------------------------------------------------- 結局、以下のよう『ImageButtonをNEWするところ』と『コレクション変数に代入するところ』 をPage_Initに移しました。 今後とも宜しくお願いします。 -------------------------------------------------------------- Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton Protected WithEvents ImageButton2 As System.Web.UI.WebControls.ImageButton Protected WithEvents ImageButton3 As System.Web.UI.WebControls.ImageButton Protected WithEvents PlaceHolder As System.Web.UI.WebControls.PlaceHolder Public ImageCol As New Collection Private Sub Page_Init( ・・・略・・・ ) Handles MyBase.Init ImageButton1 = New System.Web.UI.WebControls.ImageButton ImageButton2 = New System.Web.UI.WebControls.ImageButton ImageButton3 = New System.Web.UI.WebControls.ImageButton ImageCol.Add(ImageButton1) ImageCol.Add(ImageButton2) ImageCol.Add(ImageButton3) End Sub Private Sub Page_Load( ・・・略・・・ ) Handles MyBase.Load PlaceHolder.Controls.Add(ImageCol(1)) End Sub Private Sub ImageButton_Command( ・・・略・・・ ) Handles _ ImageButton1.Command, _ ImageButton2.Command, _ ImageButton3.Command ・ ・ ・ End Sub | ||||
|
投稿日時: 2004-05-21 17:52
お〜、これすごい。
ASP.NETで動的にというのは考えないことにしていたんですが。(方法が良く分からなかったので) 参考になりました。Qooさんありがとうございました。 | ||||
|
投稿日時: 2004-05-21 18:17
同じく。 ただ、カスタムサーバーコントロールを作っていて、Loadイベントハンドラに「初期化する為のコードを記述します」と書いてあるのが、とんでもない間違いだということには気がついた。これは、はっきり言って「コメントのバグ」ですよ |
1