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

回答編集履歴

1

追記追加

2017/12/28 05:38

投稿

退会済みユーザー
answer CHANGED
@@ -35,4 +35,170 @@
35
35
 
36
36
  ChackBox にする必要はなくて、結果を文字列として表示できれば良いのであればそれだけで済むはずです。
37
37
 
38
- どうしても CheckBox と Label を切り替えて表示したいということであれば、GridView の RowDataBound イベントのハンドラで処置するなどもう一工夫必要ですが、ASPX ページのコードの可読性は格段に上がるはずです。
38
+ どうしても CheckBox と Label を切り替えて表示したいということであれば、GridView の RowDataBound イベントのハンドラで処置するなどもう一工夫必要ですが、ASPX ページのコードの可読性は格段に上がるはずです。
39
+
40
+ **【追記】**
41
+
42
+ サンプルを作ったのでアップしておきます。Result クラスは上のものから簡略化しました。.aspx のコードで、GridView1 が文字列で表示、GridView2 が CheckBox と Label を切り替えて表示するようにしています。
43
+
44
+ **ビジネスロジック**
45
+
46
+ ```
47
+ using System;
48
+ using System.Collections.Generic;
49
+ using System.Linq;
50
+ using System.Web;
51
+
52
+ public class Result
53
+ {
54
+ public string Name { get; set; }
55
+ public string App1 { get; set; }
56
+ public string App2 { get; set; }
57
+ public string App3 { get; set; }
58
+ }
59
+
60
+ public class BusinessLogic
61
+ {
62
+ public List<Result> GetResults()
63
+ {
64
+ List<Result> results = new List<Result>()
65
+ {
66
+ new Result() { Name = "ユーザー1", App1 = "チェック未", App2 = "申請不可", App3 = "チェック未" },
67
+ new Result() { Name = "ユーザー2", App1 = "チェック済", App2 = "チェック未", App3 = "申請済" },
68
+ new Result() { Name = "ユーザー3", App1 = "チェック未", App2 = "チェック済", App3 = "チェック済" }
69
+ };
70
+
71
+ return results;
72
+ }
73
+ }
74
+
75
+ ```
76
+
77
+ **.aspx**
78
+
79
+ ```
80
+ <%@ Page Language="C#" AutoEventWireup="true"
81
+ CodeFile="0022-GridViewCheckBox.aspx.cs"
82
+ Inherits="_0022_GridViewCheckBox" %>
83
+
84
+ <!DOCTYPE html>
85
+
86
+ <html xmlns="http://www.w3.org/1999/xhtml">
87
+ <head runat="server">
88
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
89
+ <title></title>
90
+ </head>
91
+ <body>
92
+ <form id="form1" runat="server">
93
+ <asp:ObjectDataSource ID="ObjectDataSource1"
94
+ runat="server" SelectMethod="GetResults"
95
+ TypeName="BusinessLogic">
96
+ </asp:ObjectDataSource>
97
+
98
+ <asp:GridView ID="GridView1" runat="server"
99
+ AutoGenerateColumns="False"
100
+ DataSourceID="ObjectDataSource1">
101
+ <Columns>
102
+ <asp:BoundField DataField="Name" HeaderText="ユーザー名" />
103
+ <asp:BoundField DataField="App1" HeaderText="申請項目1" />
104
+ <asp:BoundField DataField="App2" HeaderText="申請項目2" />
105
+ <asp:BoundField DataField="App3" HeaderText="申請項目3" />
106
+ </Columns>
107
+
108
+ </asp:GridView>
109
+
110
+ <asp:GridView ID="GridView2" runat="server"
111
+ AutoGenerateColumns="False"
112
+ DataSourceID="ObjectDataSource1">
113
+ <Columns>
114
+ <asp:BoundField DataField="Name" HeaderText="ユーザー名" />
115
+ <asp:TemplateField HeaderText="申請項目1">
116
+ <ItemTemplate>
117
+ <asp:CheckBox ID="CheckBox1" runat="server"
118
+ Visible='<%# CheckBoxVisible((string)Eval("App1")) %>'
119
+ Checked='<%# IsChecked((string)Eval("App1")) %>' />
120
+ <asp:Label ID="Label1" runat="server" Text='<%# Eval("App1") %>'
121
+ Visible='<%# LabelVisible((string)Eval("App1")) %>'>
122
+ </asp:Label>
123
+ </ItemTemplate>
124
+ </asp:TemplateField>
125
+ <asp:TemplateField HeaderText="申請項目2">
126
+ <ItemTemplate>
127
+ <asp:CheckBox ID="CheckBox2" runat="server"
128
+ Visible='<%# CheckBoxVisible((string)Eval("App2")) %>'
129
+ Checked='<%# IsChecked((string)Eval("App2")) %>' />
130
+ <asp:Label ID="Label2" runat="server" Text='<%# Eval("App2") %>'
131
+ Visible='<%# LabelVisible((string)Eval("App2")) %>'>
132
+ </asp:Label>
133
+ </ItemTemplate>
134
+ </asp:TemplateField>
135
+ <asp:TemplateField HeaderText="申請項目3">
136
+ <ItemTemplate>
137
+ <asp:CheckBox ID="CheckBox3" runat="server"
138
+ Visible='<%# CheckBoxVisible((string)Eval("App3")) %>'
139
+ Checked='<%# IsChecked((string)Eval("App3")) %>' />
140
+ <asp:Label ID="Label3" runat="server" Text='<%# Eval("App3") %>'
141
+ Visible='<%# LabelVisible((string)Eval("App3")) %>'>
142
+ </asp:Label>
143
+ </ItemTemplate>
144
+ </asp:TemplateField>
145
+ </Columns>
146
+
147
+ </asp:GridView>
148
+ </form>
149
+ </body>
150
+ </html>
151
+
152
+ ```
153
+
154
+ **.aspx.cs**
155
+
156
+ ```
157
+ using System;
158
+ using System.Collections.Generic;
159
+ using System.Linq;
160
+ using System.Web;
161
+ using System.Web.UI;
162
+ using System.Web.UI.WebControls;
163
+
164
+ public partial class _0022_GridViewCheckBox : System.Web.UI.Page
165
+ {
166
+ protected void Page_Load(object sender, EventArgs e)
167
+ {
168
+
169
+ }
170
+
171
+ protected bool LabelVisible(string str)
172
+ {
173
+ if (str == "チェック未" || str == "チェック済")
174
+ {
175
+ return false;
176
+ }
177
+ return true;
178
+ }
179
+
180
+ protected bool CheckBoxVisible(string str)
181
+ {
182
+ if (str == "チェック未" || str == "チェック済")
183
+ {
184
+ return true;
185
+ }
186
+ return false;
187
+ }
188
+
189
+ protected bool IsChecked(string str)
190
+ {
191
+ if (str == "チェック未")
192
+ {
193
+ return false;
194
+ }
195
+
196
+ if (str == "チェック済")
197
+ {
198
+ return true;
199
+ }
200
+
201
+ return false;
202
+ }
203
+ }
204
+ ```