回答編集履歴
1
追記
test
CHANGED
@@ -15,3 +15,151 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
DropDownList サーバーコントロールを使うか、それがダメならサーバー側でのイベントに頼らない全く別の手段を考えるほかないと思います。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
****** 2017/5/5 17:40 追記 ******
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
2017/05/05 17:27 の私のコメントで「後で具体例を回答欄に書いておきます」と書きましたが、それを以下に追記します。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
DB から取得するデータを DropDownList に表示するということで、SqlDataSource コントロールと併用します。DB は Microsoft の Northwind サンプルデータベースの Categories テーブルと Products テーブルを例に使い、1 つめの DropDownList にカテゴリーを表示し、その選択を変えると自動的に 2 つめの DropDownList に選んだカテゴリーに属する商品一覧を表示するようにしています。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
Visual Studio のウィザードを利用して、イベントハンドラの中身以外は一切コードを書かずに実装しています。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
<%@ Page Language="C#" %>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
<!DOCTYPE html>
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
<script runat="server">
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
protected void ProductDripdown_SelectedIndexChanged(object sender, EventArgs e)
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
Label2.Text = "Selected Value: " + ((DropDownList)sender).SelectedValue;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
</script>
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
64
|
+
|
65
|
+
<head runat="server">
|
66
|
+
|
67
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
68
|
+
|
69
|
+
<title></title>
|
70
|
+
|
71
|
+
</head>
|
72
|
+
|
73
|
+
<body>
|
74
|
+
|
75
|
+
<form id="form1" runat="server">
|
76
|
+
|
77
|
+
<asp:SqlDataSource ID="SqlDataSource1"
|
78
|
+
|
79
|
+
runat="server"
|
80
|
+
|
81
|
+
ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
|
82
|
+
|
83
|
+
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
|
84
|
+
|
85
|
+
</asp:SqlDataSource>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
<asp:DropDownList ID="categoryDropdown"
|
90
|
+
|
91
|
+
runat="server"
|
92
|
+
|
93
|
+
AutoPostBack="True"
|
94
|
+
|
95
|
+
DataSourceID="SqlDataSource1"
|
96
|
+
|
97
|
+
DataTextField="CategoryName"
|
98
|
+
|
99
|
+
DataValueField="CategoryID">
|
100
|
+
|
101
|
+
</asp:DropDownList>
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
<br />
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
<asp:SqlDataSource ID="SqlDataSource2"
|
110
|
+
|
111
|
+
runat="server"
|
112
|
+
|
113
|
+
ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
|
114
|
+
|
115
|
+
SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] WHERE ([CategoryID] = @CategoryID)">
|
116
|
+
|
117
|
+
<SelectParameters>
|
118
|
+
|
119
|
+
<asp:ControlParameter ControlID="categoryDropdown"
|
120
|
+
|
121
|
+
Name="CategoryID"
|
122
|
+
|
123
|
+
PropertyName="SelectedValue"
|
124
|
+
|
125
|
+
Type="Int32" />
|
126
|
+
|
127
|
+
</SelectParameters>
|
128
|
+
|
129
|
+
</asp:SqlDataSource>
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
<asp:DropDownList ID="ProductDripdown"
|
134
|
+
|
135
|
+
runat="server"
|
136
|
+
|
137
|
+
AutoPostBack="True"
|
138
|
+
|
139
|
+
DataSourceID="SqlDataSource2"
|
140
|
+
|
141
|
+
DataTextField="ProductName"
|
142
|
+
|
143
|
+
DataValueField="ProductID"
|
144
|
+
|
145
|
+
OnSelectedIndexChanged="ProductDripdown_SelectedIndexChanged">
|
146
|
+
|
147
|
+
</asp:DropDownList>
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
<br />
|
152
|
+
|
153
|
+
<asp:Label ID="Label2" runat="server"></asp:Label>
|
154
|
+
|
155
|
+
</form>
|
156
|
+
|
157
|
+
</body>
|
158
|
+
|
159
|
+
</html>
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
見慣れないかもしれませんが、上記は Web サイトプロジェクトでコードは分離していません。
|