teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コードま間違いの修正

2016/09/12 00:00

投稿

twck
twck

スコア314

answer CHANGED
@@ -21,11 +21,11 @@
21
21
  If reader.Read Then
22
22
 
23
23
  'FormView1の中から各コントロールを探して値をセットする
24
- DirectCast(FormView1.FindControl("DropDownList1"), DropDownList).SelectedValue = "はい"
24
+ DirectCast(FormView1.FindControl("DropDownList1"), DropDownList).SelectedValue = reader("result1").ToString
25
- DirectCast(FormView1.FindControl("DropDownList2"), DropDownList).SelectedValue = "いいえ"
25
+ DirectCast(FormView1.FindControl("DropDownList2"), DropDownList).SelectedValue = reader("result2").ToString
26
- DirectCast(FormView1.FindControl("DropDownList3"), DropDownList).SelectedValue = "はい"
26
+ DirectCast(FormView1.FindControl("DropDownList3"), DropDownList).SelectedValue = reader("result3").ToString
27
- DirectCast(FormView1.FindControl("DropDownList4"), DropDownList).SelectedValue = "はい"
27
+ DirectCast(FormView1.FindControl("DropDownList4"), DropDownList).SelectedValue = reader("result4").ToString
28
- DirectCast(FormView1.FindControl("DropDownList5"), DropDownList).SelectedValue = "未回答"
28
+ DirectCast(FormView1.FindControl("DropDownList5"), DropDownList).SelectedValue = reader("result5").ToString
29
29
  End If
30
30
  End Using
31
31
  End Using

1

コードの追加

2016/09/12 00:00

投稿

twck
twck

スコア314

answer CHANGED
@@ -9,6 +9,28 @@
9
9
  ```VB
10
10
  Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
11
11
 
12
+ '新規登録モードのときのみ
13
+ If FormView1.CurrentMode = FormViewMode.Insert Then
14
+
15
+ '初期値用のレコードを読み込んで各コントロールにセットする
16
+ Using db As New System.Data.SqlClient.SqlConnection(接続文字列)
17
+ db.Open()
18
+ Dim cmd = db.CreateCommand()
19
+ cmd.CommandText = "SELECT * FROM テーブル名 WHERE ID = 初期値のレコードのキー"
20
+ Using reader = cmd.ExecuteReader
21
+ If reader.Read Then
22
+
23
+ 'FormView1の中から各コントロールを探して値をセットする
24
+ DirectCast(FormView1.FindControl("DropDownList1"), DropDownList).SelectedValue = "はい"
25
+ DirectCast(FormView1.FindControl("DropDownList2"), DropDownList).SelectedValue = "いいえ"
26
+ DirectCast(FormView1.FindControl("DropDownList3"), DropDownList).SelectedValue = "はい"
27
+ DirectCast(FormView1.FindControl("DropDownList4"), DropDownList).SelectedValue = "はい"
28
+ DirectCast(FormView1.FindControl("DropDownList5"), DropDownList).SelectedValue = "未回答"
29
+ End If
30
+ End Using
31
+ End Using
32
+ End If
33
+
12
34
  '編集モードのときのみ
13
35
  If FormView1.CurrentMode = FormViewMode.Edit Then
14
36