回答編集履歴
2
具体例追記
test
CHANGED
@@ -3,3 +3,123 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
お試しください。ダメならその旨連絡ください。実際にコードを書いて検証してみます。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
【2016/10/6 10:50 追記】
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
私の 2016/10/06 10:24 のコメントで「具体例はあとで回答欄に追記しておきます」と書きましたように、すべて Page.Load イベントで行う例を書いておきます。データベースは Microsoft が提供しているサンプルデータベース Northwind の Employees テーブルを使っています。Button はポストバックした時の動作の確認用です。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
<%@ Page Language="C#" %>
|
20
|
+
|
21
|
+
<%@ Import Namespace="System.Data" %>
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
26
|
+
|
27
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
<script runat="server">
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
// 2016/10/5 teratail の検証用
|
36
|
+
|
37
|
+
// https://teratail.com/questions/50422
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
protected void DropDownList1_DataBound(object sender, EventArgs e)
|
42
|
+
|
43
|
+
{
|
44
|
+
|
45
|
+
// EmployeeID = 7 の LastName は King
|
46
|
+
|
47
|
+
//ListItem item = DropDownList1.Items.FindByValue("7");
|
48
|
+
|
49
|
+
//int index = DropDownList1.Items.IndexOf(item);
|
50
|
+
|
51
|
+
//DropDownList1.SelectedIndex = index;
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
protected void Page_Load(object sender, EventArgs e)
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
if (!Page.IsPostBack)
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
DropDownList1.DataSource =
|
66
|
+
|
67
|
+
(DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
|
68
|
+
|
69
|
+
DropDownList1.DataBind();
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// EmployeeID = 7 の LastName は King
|
74
|
+
|
75
|
+
ListItem item = DropDownList1.Items.FindByValue("7");
|
76
|
+
|
77
|
+
int index = DropDownList1.Items.IndexOf(item);
|
78
|
+
|
79
|
+
DropDownList1.SelectedIndex = index;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
</script>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
90
|
+
|
91
|
+
<head runat="server">
|
92
|
+
|
93
|
+
<title></title>
|
94
|
+
|
95
|
+
</head>
|
96
|
+
|
97
|
+
<body>
|
98
|
+
|
99
|
+
<form id="form1" runat="server">
|
100
|
+
|
101
|
+
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
|
102
|
+
|
103
|
+
ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
|
104
|
+
|
105
|
+
SelectCommand="SELECT [EmployeeID], [LastName] FROM [Employees]">
|
106
|
+
|
107
|
+
</asp:SqlDataSource>
|
108
|
+
|
109
|
+
<asp:DropDownList ID="DropDownList1" runat="server"
|
110
|
+
|
111
|
+
DataTextField="LastName"
|
112
|
+
|
113
|
+
DataValueField="EmployeeID" OnDataBound="DropDownList1_DataBound">
|
114
|
+
|
115
|
+
</asp:DropDownList>
|
116
|
+
|
117
|
+
<asp:Button ID="Button1" runat="server" Text="Button" />
|
118
|
+
|
119
|
+
</form>
|
120
|
+
|
121
|
+
</body>
|
122
|
+
|
123
|
+
</html>
|
124
|
+
|
125
|
+
```
|
1
誤記訂正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Page_Load の時点では、質問者さんの言われるとおり、DropDownList にデータバインドされていませんので、Page_load ではなく DropDown
|
1
|
+
Page_Load の時点では、質問者さんの言われるとおり、DropDownList にデータバインドされていませんので、Page_load ではなく DropDownList.DataBound イベントのハンドラで設定すればうまくいくのではないかと思います。
|
2
2
|
|
3
3
|
|
4
4
|
|