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

回答編集履歴

1

追記

2020/09/01 01:49

投稿

退会済みユーザー
answer CHANGED
@@ -6,4 +6,165 @@
6
6
 
7
7
  データバインド式を使って ID='<%# Eval("CheckBoxId") %>' のように ID を設定することはできないので(質問者さんが経験したエラーになる)、Repeater.ItemDataBound イベントのイベントハンドラで設定するほかなさそうです。
8
8
 
9
- イベントハンドラの引数 RepeaterItemEventArgs の Item プロパティで RepeaterItem を取得し、その中に含まれるコントロールの中から CheckBox を探して、その ID プロパティに T クラスに含まれる文字列データを設定するようにしてみてください。
9
+ イベントハンドラの引数 RepeaterItemEventArgs の Item プロパティで RepeaterItem を取得し、その中に含まれるコントロールの中から CheckBox を探して、その ID プロパティに T クラスに含まれる文字列データを設定するようにしてみてください。
10
+
11
+ **【2020/9/1 追記】**
12
+
13
+ 上のように言っておいて、もしできなかったら何ですので検証してみました。検証に使ったコードと結果の画像を貼っておきます。上の回答で書いた List<T> 型のオブジェクトというのは下の .aspx.cs の CreateDataSource メソッドで作られる List<SampleData> で、List<T> の T クラスは SampleData になります。
14
+
15
+ **.aspx.cs**
16
+
17
+ ```
18
+ using System;
19
+ using System.Collections.Generic;
20
+ using System.Linq;
21
+ using System.Web;
22
+ using System.Web.UI;
23
+ using System.Web.UI.WebControls;
24
+
25
+ namespace WebApplication1
26
+ {
27
+ public partial class WebForm9 : System.Web.UI.Page
28
+ {
29
+ protected void Page_Load(object sender, EventArgs e)
30
+ {
31
+ if (!IsPostBack)
32
+ {
33
+ Repeater1.DataSource = CreateDataSource();
34
+ Repeater1.DataBind();
35
+ }
36
+ }
37
+
38
+ protected List<SampleData> CreateDataSource()
39
+ {
40
+ var list = new List<SampleData>();
41
+
42
+ for (int i = 0; i < 5; i++)
43
+ {
44
+ var data = new SampleData
45
+ {
46
+ Id = i,
47
+ Name = "Name-" + i,
48
+ Price = 100m * (i + 1),
49
+ CheckBoxId = "CheckBox" + i
50
+ };
51
+ list.Add(data);
52
+ }
53
+
54
+ return list;
55
+ }
56
+
57
+ protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
58
+ {
59
+ if (e.Item.ItemType == ListItemType.Item ||
60
+ e.Item.ItemType == ListItemType.AlternatingItem)
61
+ {
62
+ foreach (Control ctrl in e.Item.Controls)
63
+ {
64
+ if (ctrl is CheckBox)
65
+ {
66
+ ((CheckBox)ctrl).ID = ((SampleData)e.Item.DataItem).CheckBoxId;
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ public class SampleData
75
+ {
76
+ public int Id { get; set; }
77
+
78
+ public string Name { get; set; }
79
+
80
+ public decimal Price { get; set; }
81
+
82
+ public string CheckBoxId { get; set; }
83
+ }
84
+ }
85
+ ```
86
+
87
+
88
+ **.aspx**
89
+
90
+ ```
91
+ <%@ Page Language="C#" AutoEventWireup="true"
92
+ CodeBehind="WebForm9.aspx.cs"
93
+ Inherits="WebApplication1.WebForm9" %>
94
+
95
+ <!DOCTYPE html>
96
+
97
+ <html xmlns="http://www.w3.org/1999/xhtml">
98
+ <head runat="server">
99
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
100
+ <title></title>
101
+ <style type="text/css">
102
+ table.style1
103
+ {
104
+ border-style: solid;
105
+ border-width: 2px;
106
+ border-color: Black;
107
+ text-align: center;
108
+ border-collapse: collapse;
109
+ }
110
+
111
+ table.style1 th
112
+ {
113
+ border-style: solid;
114
+ border-width: 2px 1px 2px 1px;
115
+ border-color: Black;
116
+ background-color: #6699FF;
117
+ color: #FFFFFF;
118
+ }
119
+
120
+ table.style1 td
121
+ {
122
+ border-style: solid;
123
+ border-width: 1px;
124
+ border-color: Black;
125
+ }
126
+ </style>
127
+ </head>
128
+ <body>
129
+ <form id="form1" runat="server">
130
+ <div>
131
+ <asp:Repeater ID="Repeater1" runat="server"
132
+ OnItemDataBound="Repeater1_ItemDataBound">
133
+ <HeaderTemplate>
134
+ <table class="style1">
135
+ <tr>
136
+ <th>ID</th>
137
+ <th>Name</td>
138
+ <th>Price</th>
139
+ <th>Check</th>
140
+ </tr>
141
+ </HeaderTemplate>
142
+
143
+ <ItemTemplate>
144
+ <tr>
145
+ <td><%# Eval("ID")%></td>
146
+ <td><%# Eval("Name")%></td>
147
+ <td><%# Eval("Price")%></td>
148
+ <td>
149
+ <asp:CheckBox runat="server" ClientIDMode="Static" />
150
+ </td>
151
+ </tr>
152
+
153
+ </ItemTemplate>
154
+ <FooterTemplate>
155
+ </table>
156
+ </FooterTemplate>
157
+ </asp:Repeater>
158
+ </div>
159
+ </form>
160
+ </body>
161
+ </html>
162
+ ```
163
+
164
+ **結果**
165
+
166
+ ![イメージ説明](284edefc3c752a1dcb60f08f40e9d88d.jpeg)
167
+
168
+ **html**
169
+
170
+ ![イメージ説明](9f70670c2eea38e0bfeeb6e04510220d.jpeg)