質問編集履歴
1
詳細情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
ASP.NET C#でListViewとNorthWindを用いてテーブルの内容を表示し、削除・編集・挿入できるプログラムを作成しています。
|
3
|
+
ASP.NET C#でListViewとNorthWind(テーブル:Region)を用いてテーブルの内容を表示し、削除・編集・挿入できるプログラムを作成しています。検証コントロールは使用していません。
|
4
4
|
テーブルの表示、削除、編集、挿入の実装ができ、それぞれのコマンドを実装する前にメッセージを表示することができました。ですが、挿入のときに、テキストボックスの中身が空だとエラーが発生(error:NULLを挿入できません)しました。
|
5
5
|
これを、テキストボックスの中身が空のときにエラーをキャッチし、メッセージを表示する方法が分かりません。
|
6
6
|
|
@@ -12,28 +12,152 @@
|
|
12
12
|
以下のようにif文を記述したところ、ClientScript~のところで「到達できないコードが検出されました」と出てしまいました。
|
13
13
|
|
14
14
|
```
|
15
|
-
<InsertItemTemplate>
|
16
|
-
<tr style="">
|
17
|
-
<td>
|
18
|
-
|
15
|
+
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="****" %>
|
19
|
-
<% if("RegionIDTextBox" == "")
|
20
|
-
{
|
21
|
-
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "alert('入力されていません。')", true)
|
22
|
-
return;
|
23
|
-
}
|
24
|
-
%>
|
25
16
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
<
|
17
|
+
<!DOCTYPE html>
|
18
|
+
|
19
|
+
<html xmlns="****">
|
20
|
+
<head runat="server">
|
21
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
22
|
+
<title></title>
|
23
|
+
</head>
|
24
|
+
<body>
|
25
|
+
<form id="form1" runat="server">
|
26
|
+
<div>
|
27
|
+
ASP.NET Do-It-Yourself</div>
|
28
|
+
<hr/>
|
29
|
+
<asp:ListView ID="ListView1" runat="server" DataKeyNames="RegionID" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
|
30
|
+
<AlternatingItemTemplate>
|
31
|
+
<tr style="background-color: #FFFFFF;color: #284775;">
|
32
|
+
<td>
|
33
|
+
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="削除"/>
|
34
|
+
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
|
35
|
+
</td>
|
36
|
+
<td>
|
37
|
+
<asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' />
|
38
|
+
</td>
|
39
|
+
<td>
|
40
|
+
<asp:Label ID="RegionDescriptionLabel" runat="server" Text='<%# Eval("RegionDescription") %>' />
|
41
|
+
</td>
|
42
|
+
</tr>
|
43
|
+
</AlternatingItemTemplate>
|
44
|
+
<EditItemTemplate>
|
45
|
+
<tr style="background-color: #999999;">
|
46
|
+
<td>
|
47
|
+
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" OnClientClick="return confirm('更新しますか?')"/>
|
48
|
+
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="キャンセル" />
|
49
|
+
</td>
|
50
|
+
<td>
|
51
|
+
<asp:Label ID="RegionIDLabel1" runat="server" Text='<%# Eval("RegionID") %>' />
|
52
|
+
</td>
|
53
|
+
<td>
|
54
|
+
<asp:TextBox ID="RegionDescriptionTextBox" runat="server" Text='<%# Bind("RegionDescription") %>' />
|
55
|
+
</td>
|
56
|
+
</tr>
|
57
|
+
</EditItemTemplate>
|
58
|
+
<EmptyDataTemplate>
|
59
|
+
<table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
|
60
|
+
<tr>
|
61
|
+
<td>データは返されませんでした。</td>
|
62
|
+
</tr>
|
63
|
+
</table>
|
64
|
+
</EmptyDataTemplate>
|
65
|
+
<InsertItemTemplate>
|
66
|
+
<tr style="">
|
67
|
+
<td>
|
68
|
+
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="挿入" OnClientClick="return confirm('挿入しますか?')"/>
|
69
|
+
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="クリア" OnClientClick="return confirm('クリアしますか?')"/>
|
70
|
+
<%if ("RegionIDTextBox" == "")
|
71
|
+
{
|
72
|
+
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "alert('入力されていません。');", true);
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
%>
|
76
|
+
|
77
|
+
</td>
|
78
|
+
<td>
|
79
|
+
<asp:TextBox ID="RegionIDTextBox" runat="server" Text='<%# Bind("RegionID") %>' />
|
80
|
+
</td>
|
81
|
+
<td>
|
82
|
+
<asp:TextBox ID="RegionDescriptionTextBox" runat="server" Text='<%# Bind("RegionDescription") %>' />
|
83
|
+
</td>
|
84
|
+
</tr>
|
85
|
+
</InsertItemTemplate>
|
86
|
+
<ItemTemplate>
|
87
|
+
<tr style="background-color: #E0FFFF;color: #333333;">
|
88
|
+
<td>
|
89
|
+
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="削除" OnClientClick="return confirm('削除しますか?')"/>
|
90
|
+
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
|
91
|
+
</td>
|
92
|
+
<td>
|
93
|
+
<asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' />
|
94
|
+
</td>
|
95
|
+
<td>
|
96
|
+
<asp:Label ID="RegionDescriptionLabel" runat="server" Text='<%# Eval("RegionDescription") %>' />
|
97
|
+
</td>
|
98
|
+
</tr>
|
99
|
+
</ItemTemplate>
|
100
|
+
<LayoutTemplate>
|
101
|
+
<table runat="server">
|
102
|
+
<tr runat="server">
|
103
|
+
<td runat="server">
|
104
|
+
<table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
|
105
|
+
<tr runat="server" style="background-color: #E0FFFF;color: #333333;">
|
106
|
+
<th runat="server"></th>
|
107
|
+
<th runat="server">RegionID</th>
|
108
|
+
<th runat="server">RegionDescription</th>
|
109
|
+
</tr>
|
110
|
+
<tr id="itemPlaceholder" runat="server">
|
111
|
+
</tr>
|
112
|
+
</table>
|
113
|
+
</td>
|
114
|
+
</tr>
|
115
|
+
<tr runat="server">
|
116
|
+
<td runat="server" style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
|
117
|
+
<asp:DataPager ID="DataPager1" runat="server">
|
118
|
+
<Fields>
|
119
|
+
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
|
120
|
+
</Fields>
|
121
|
+
</asp:DataPager>
|
122
|
+
</td>
|
123
|
+
</tr>
|
124
|
+
</table>
|
125
|
+
</LayoutTemplate>
|
126
|
+
<SelectedItemTemplate>
|
127
|
+
<tr style="background-color: #E2DED6;font-weight: bold;color: #333333;">
|
128
|
+
<td>
|
129
|
+
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="削除"/>
|
130
|
+
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
|
131
|
+
</td>
|
132
|
+
<td>
|
133
|
+
<asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' />
|
134
|
+
</td>
|
135
|
+
<td>
|
136
|
+
<asp:Label ID="RegionDescriptionLabel" runat="server" Text='<%# Eval("RegionDescription") %>' />
|
137
|
+
</td>
|
138
|
+
</tr>
|
139
|
+
</SelectedItemTemplate>
|
140
|
+
</asp:ListView>
|
141
|
+
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>" DeleteCommand="DELETE FROM [Region] WHERE [RegionID] = @RegionID" InsertCommand="INSERT INTO [Region] ([RegionID], [RegionDescription]) VALUES (@RegionID, @RegionDescription)" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString1.ProviderName %>" SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]" UpdateCommand="UPDATE [Region] SET [RegionDescription] = @RegionDescription WHERE [RegionID] = @RegionID">
|
142
|
+
<DeleteParameters>
|
143
|
+
<asp:Parameter Name="RegionID" Type="Int32" />
|
144
|
+
</DeleteParameters>
|
145
|
+
<InsertParameters>
|
146
|
+
<asp:Parameter Name="RegionID" Type="Int32" />
|
147
|
+
<asp:Parameter Name="RegionDescription" Type="String" />
|
148
|
+
</InsertParameters>
|
149
|
+
<UpdateParameters>
|
150
|
+
<asp:Parameter Name="RegionDescription" Type="String" />
|
151
|
+
<asp:Parameter Name="RegionID" Type="Int32" />
|
152
|
+
</UpdateParameters>
|
153
|
+
</asp:SqlDataSource>
|
154
|
+
</form>
|
155
|
+
</body>
|
156
|
+
</html>
|
157
|
+
|
35
158
|
```
|
36
159
|
|
37
160
|
### 補足情報(FW/ツールのバージョンなど)
|
38
161
|
|
39
|
-
SQLserver ASP.NET、C#、Visual Studio2019、Northwind
|
162
|
+
SQLserver ASP.NET、C#、Visual Studio2019(16.6.2)、Northwind(テーブル:Region)
|
163
|
+
.NET Framework(4.8) sqlDataSource
|