回答編集履歴

1

追伸追加

2016/10/31 08:31

投稿

退会済みユーザー
test CHANGED
@@ -27,3 +27,145 @@
27
27
 
28
28
 
29
29
  データソースコントロールを使うことで、データバインドするタイミングとかその他いろいろなことを自動的に行ってくれますので、特に理由がない限り使うべきなのですが。
30
+
31
+
32
+
33
+ 【2016/10/31 17:30 追伸】
34
+
35
+
36
+
37
+ ページ B は、私の上の回答で述べたように、以下のようにデータソースコントロールを利用して作れば、データバインドのタイミングとかに一切悩む必要はなく、Page_Load 内のコード以外は一行も自力で書く必要もなく、簡単に作れると思います。検討してみてください。
38
+
39
+
40
+
41
+ (注:コードの中の Customers, Orders テーブルは Microsoft が提供する Northwind サンプルデータベースのものです)
42
+
43
+
44
+
45
+ ```
46
+
47
+ <script runat="server">
48
+
49
+
50
+
51
+ // 2016/10/31 teratail の検証用
52
+
53
+ // https://teratail.com/questions/53395
54
+
55
+
56
+
57
+ protected void Page_Load(object sender, EventArgs e)
58
+
59
+ {
60
+
61
+ if (!Page.IsPostBack)
62
+
63
+ {
64
+
65
+ //int selectedIndex = (int)Session["Index"]; の代わりに直接設定
66
+
67
+ int selectedIndex = 5;
68
+
69
+
70
+
71
+ DropDownList1.SelectedIndex = selectedIndex;
72
+
73
+ }
74
+
75
+ }
76
+
77
+ </script>
78
+
79
+
80
+
81
+ <html xmlns="http://www.w3.org/1999/xhtml">
82
+
83
+ <head runat="server">
84
+
85
+ <title></title>
86
+
87
+ </head>
88
+
89
+ <body>
90
+
91
+ <form id="form1" runat="server">
92
+
93
+
94
+
95
+ <asp:SqlDataSource ID="SqlDataSource1" runat="server"
96
+
97
+ ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
98
+
99
+ SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]">
100
+
101
+ </asp:SqlDataSource>
102
+
103
+ <asp:DropDownList ID="DropDownList1" runat="server"
104
+
105
+ DataSourceID="SqlDataSource1" DataTextField="CompanyName"
106
+
107
+ DataValueField="CustomerID" AutoPostBack="True">
108
+
109
+ </asp:DropDownList>
110
+
111
+
112
+
113
+ <asp:SqlDataSource ID="SqlDataSource2" runat="server"
114
+
115
+ ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
116
+
117
+ SelectCommand="SELECT [OrderID], [CustomerID], [ShippedDate], [Freight], [EmployeeID]
118
+
119
+ FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
120
+
121
+ <SelectParameters>
122
+
123
+ <asp:ControlParameter ControlID="DropDownList1" Name="CustomerID"
124
+
125
+ PropertyName="SelectedValue" Type="String" />
126
+
127
+ </SelectParameters>
128
+
129
+ </asp:SqlDataSource>
130
+
131
+ <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
132
+
133
+ DataKeyNames="OrderID" DataSourceID="SqlDataSource2">
134
+
135
+ <Columns>
136
+
137
+ <asp:BoundField DataField="OrderID" HeaderText="OrderID"
138
+
139
+ InsertVisible="False"
140
+
141
+ ReadOnly="True" SortExpression="OrderID" />
142
+
143
+ <asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
144
+
145
+ SortExpression="CustomerID" />
146
+
147
+ <asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate"
148
+
149
+ SortExpression="ShippedDate" />
150
+
151
+ <asp:BoundField DataField="Freight" HeaderText="Freight"
152
+
153
+ SortExpression="Freight" />
154
+
155
+ <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
156
+
157
+ SortExpression="EmployeeID" />
158
+
159
+ </Columns>
160
+
161
+ </asp:GridView>
162
+
163
+
164
+
165
+ </form>
166
+
167
+ </body>
168
+
169
+ </html>
170
+
171
+ ```