" />
二嘎的学习笔记
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 8 文章- 0 评论- 5
DropDownList的动态加载,detailview使用以及FileUpload的上传
所有代码
后台
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Collections;
5
using
System.Web;
6
using
System.Web.Security;
7
using
System.Web.UI;
8
using
System.IO;
9
using
System.Web.Configuration;
10
using
System.Web.UI.WebControls;
11
using
System.Web.UI.WebControls.WebParts;
12
using
System.Web.UI.HtmlControls;
13
using
ShoppingChain.ManageSQLServerDAL;
14
using
ShoppingChain.ManageModel;
15
using
System.Collections.Generic;
16
17
public
partial
class
Manage_BusinessmanNewP : System.Web.UI.Page
18
{
19
protected
void
Page_Load(
object
sender, EventArgs e)
20
{
21
if
(
!
IsPostBack)
22
{
23
ProductInfo info
=
new
ProductInfo();
24
25
if
(Request.Cookies[
"
uname
"
]
!=
null
)
26
{
27
HttpCookie cookie
=
Request.Cookies[
"
uname
"
];
28
string
uname
=
Server.HtmlEncode(cookie.Value);
29
Product p
=
new
Product();
30
string
ucate
=
p.GetBCatebyUname(uname);
31
//
拼表名
32
info.GoodsinfoName
=
"
GoodsInfo_
"
+
ucate;
33
info.GoodsdetailName
=
"
GoodsDetail_
"
+
ucate;
34
ViewState[
"
ucate
"
]
=
ucate;
35
}
36
string
edit
=
Request.QueryString[
"
edit
"
];
37
if
(edit
==
null
)
38
{
39
DetailsView1.DefaultMode
=
DetailsViewMode.Edit;
40
41
}
42
else
43
{
44
DetailsView1.DefaultMode
=
DetailsViewMode.Insert;
45
46
}
47
}
48
}
49
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
50
{
51
52
}
53
protected
void
DetailsView1_ItemInserted(
object
sender, DetailsViewInsertedEventArgs e)
54
{
55
//
insert之后执行,图片上传控制
56
HttpRuntimeSection h
=
new
HttpRuntimeSection();
57
58
FileUpload f1
=
(FileUpload)DetailsView1.Rows[
10
].FindControl(
"
FileUpload1
"
);
59
FileUpload f2
=
(FileUpload)DetailsView1.Rows[
10
].FindControl(
"
FileUpload2
"
);
60
FileUpload f3
=
(FileUpload)DetailsView1.Rows[
10
].FindControl(
"
FileUpload3
"
);
61
FileUpload f4
=
(FileUpload)DetailsView1.Rows[
10
].FindControl(
"
FileUpload4
"
);
62
FileUpload f5
=
(FileUpload)DetailsView1.Rows[
10
].FindControl(
"
FileUpload5
"
);
63
64
65
bool
file1OK
=
false
;
66
bool
file2OK
=
false
;
67
bool
file3OK
=
false
;
68
bool
file4OK
=
false
;
69
bool
file5OK
=
false
;
70
String path
=
Server.MapPath(
"
~/image/
"
);
71
String fileExtension;
72
if
(f1.HasFile)
73
{
74
fileExtension
=
Path.GetExtension(f1.FileName).ToLower();
75
String[] allowedExtensions
=
{
"
.gif
"
,
"
.png
"
,
"
.jpeg
"
,
"
.jpg
"
}
;
76
77
for
(
int
i
=
0
; i
<
allowedExtensions.Length; i
++
)
78
{
79
if
(fileExtension
==
allowedExtensions[i]
&&
h.MaxRequestLength
<=
500
)
80
{
81
file1OK
=
true
;
82
}
83
}
84
}
85
if
(file1OK)
86
{
87
try
88
{
89
f1.PostedFile.SaveAs(path
+
f1.FileName);
90
}
91
catch
(Exception ex)
92
{
93
Response.Write(
"
<script>window.alert('图片上传失败!')<script>
"
);
94
}
95
}
96
else
97
{
98
Response.Write(
"
<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>
"
);
99
}
100
101
if
(f2.HasFile)
102
{
103
fileExtension
=
Path.GetExtension(f2.FileName).ToLower();
104
String[] allowedExtensions
=
{
"
.gif
"
,
"
.png
"
,
"
.jpeg
"
,
"
.jpg
"
}
;
105
106
for
(
int
i
=
0
; i
<
allowedExtensions.Length; i
++
)
107
{
108
if
(fileExtension
==
allowedExtensions[i]
&&
h.MaxRequestLength
<=
500
)
109
{
110
file2OK
=
true
;
111
}
112
}
113
}
114
if
(file2OK)
115
{
116
try
117
{
118
f2.PostedFile.SaveAs(path
+
f2.FileName);
119
}
120
catch
(Exception ex)
121
{
122
Response.Write(
"
<script>window.alert('图片上传失败!')<script>
"
);
123
}
124
}
125
else
126
{
127
Response.Write(
"
<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>
"
);
128
}
129
130
if
(f3.HasFile)
131
{
132
fileExtension
=
Path.GetExtension(f3.FileName).ToLower();
133
String[] allowedExtensions
=
{
"
.gif
"
,
"
.png
"
,
"
.jpeg
"
,
"
.jpg
"
}
;
134
135
for
(
int
i
=
0
; i
<
allowedExtensions.Length; i
++
)
136
{
137
if
(fileExtension
==
allowedExtensions[i]
&&
h.MaxRequestLength
<=
500
)
138
{
139
file3OK
=
true
;
140
}
141
}
142
}
143
if
(file3OK)
144
{
145
try
146
{
147
f3.PostedFile.SaveAs(path
+
f3.FileName);
148
}
149
catch
(Exception ex)
150
{
151
Response.Write(
"
<script>window.alert('图片上传失败!')<script>
"
);
152
}
153
}
154
else
155
{
156
Response.Write(
"
<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>
"
);
157
}
158
159
if
(f4.HasFile)
160
{
161
fileExtension
=
Path.GetExtension(f4.FileName).ToLower();
162
String[] allowedExtensions
=
{
"
.gif
"
,
"
.png
"
,
"
.jpeg
"
,
"
.jpg
"
}
;
163
164
for
(
int
i
=
0
; i
<
allowedExtensions.Length; i
++
)
165
{
166
if
(fileExtension
==
allowedExtensions[i]
&&
h.MaxRequestLength
<=
500
)
167
{
168
file4OK
=
true
;
169
}
170
}
171
}
172
if
(file4OK)
173
{
174
try
175
{
176
f4.PostedFile.SaveAs(path
+
f4.FileName);
177
}
178
catch
(Exception ex)
179
{
180
Response.Write(
"
<script>window.alert('图片上传失败!')<script>
"
);
181
}
182
}
183
else
184
{
185
Response.Write(
"
<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>
"
);
186
}
187
188
if
(f5.HasFile)
189
{
190
fileExtension
=
Path.GetExtension(f5.FileName).ToLower();
191
String[] allowedExtensions
=
{
"
.gif
"
,
"
.png
"
,
"
.jpeg
"
,
"
.jpg
"
}
;
192
193
for
(
int
i
=
0
; i
<
allowedExtensions.Length; i
++
)
194
{
195
if
(fileExtension
==
allowedExtensions[i]
&&
h.MaxRequestLength
<=
500
)
196
{
197
file5OK
=
true
;
198
}
199
}
200
}
201
if
(file5OK)
202
{
203
try
204