メモ:フォーム間のデータ渡しって、どうすればいいんですか?

質問されたので書いておく。が、最適解ではない。

      • -

1.WebForm1、WebForm2を作成。
2.WebForm1にTextBoxを3つ、ボタンをひとつ貼る
3.WebForm2にLabelを3つ貼る
4.WebForm1に以下のコードを追加


001 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
002
003 Server.Transfer("WebForm2.aspx")
004 End Sub
005 Property getValues() As Collection
006 Get
007 Dim myCollection As Collection = New Collection
008 myCollection.Add(TextBox1.Text, 1)
009 myCollection.Add(TextBox2.Text, 2)
010 myCollection.Add(TextBox3.Text, 3)
011 Return myCollection
012 End Get
013 Set(ByVal Value As Collection)
014
015 End Set
016 End Property
5.WebForm2に以下のコードを追加

001 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
002 ' ページを初期化するユーザー コードをここに挿入します。
003 Dim sourcePage As Object = context.Handler
004 If TypeName(sourcePage) = "WebForm1_aspx" Then
005 'コレクションを使って値をゲット
006 Response.Write("WebForm1からきました")
007 Dim col As Collection = CType(sourcePage, WebForm1).getValues
008 Label1.Text = col.Item(1)
009 Label2.Text = col.Item(2)
010 Label3.Text = col.Item(3)
011 Else
012 Response.Write("WebForm1からきませんでした")
013 End If
014 End Sub