-
Notifications
You must be signed in to change notification settings - Fork 0
/
addFlower.aspx.cs
196 lines (176 loc) · 6.49 KB
/
addFlower.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class addFlower : System.Web.UI.Page
{
/// <summary>
/// 绑定下拉列表
/// </summary>
private void BindDrp1 ()
{
DataSet ds = DBHelperAccess.GetList("select * from type where lx='0'");
drp1.DataValueField = "id";
drp1.DataTextField = "lxmc";
drp1.DataSource = ds.Tables[0];
drp1.DataBind();
}
private void BindDrp2 ()
{
DataSet ds = DBHelperAccess.GetList("select * from type where lx='" + drp1.SelectedValue + "' and zl='1'");
drp2.DataValueField = "id";
drp2.DataTextField = "lxmc";
drp2.DataSource = ds.Tables[0];
drp2.DataBind();
}
protected void Page_Load (object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Response.Write("<script>alert('登录超时,请从新登录!')</script>");
Response.Redirect("login.aspx");
return;
}
if (!IsPostBack)
{
BindDrp1();
BindDrp2();
string id = Request.QueryString["id"];
if (!string.IsNullOrEmpty(id))
{
DataSet dataSet = DBHelperAccess.GetList("select top 1 * from flower where id=" + id);
drp1.SelectedValue = dataSet.Tables[0].Rows[0]["lx"].ToString();
BindDrp2();
drp2.SelectedValue = dataSet.Tables[0].Rows[0]["zl"].ToString();
txt_mc.Value = dataSet.Tables[0].Rows[0]["mc"].ToString();
txt_gg.Value = dataSet.Tables[0].Rows[0]["gg"].ToString();
ck_tj.Checked = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["tj"] ?? false);
}
}
}
protected void btn_save_Click (object sender, EventArgs e)
{
string lx = drp1.SelectedValue;
string zl = drp2.SelectedValue;
string mc = txt_mc.Value.Trim();
string gg = txt_gg.Value.Trim();
bool tj = ck_tj.Checked;
if (mc == "")
{
Response.Write("<script>alert('鲜花名称不能为空!')</script>");
return;
}
else if (gg == "")
{
Response.Write("<script>alert('鲜花规格不能为空!')</script>");
return;
}
string tp = UpImg(); // 图片
if (tp == null)
{
return;
}
string str = "";
string tip = "";
string id = Request.QueryString["id"];
int i = 0;
if (!string.IsNullOrEmpty(id)) // 有id,是修改数据操作,没id,是新增操作
{
// 验证重名
int haveFlower = Convert.ToInt32(DBHelperAccess.GetSingle("select count(1) from flower where mc='" + mc + "' and lx=" + lx + " and id<>" + id));
if (haveFlower > 0)
{
Response.Write("<script>alert('该鲜花名已存在,请跟换花名!')</script>");
return;
}
tip = "修改";
if (string.IsNullOrEmpty(img_tp.PostedFile.FileName))
{
str = "update flower set lx=" + lx + ",zl=" + zl + ",mc='" + mc + "',gg='" + gg + "',tj=" + ck_tj.Checked + " where id=" + id.ToString();
}
else
{
str = "update flower set lx=" + lx + ",zl=" + zl + ",mc='" + mc + "',gg='" + gg + "',tp='" + tp + "',tj=" + ck_tj.Checked + " where id=" + id.ToString();
}
i = DBHelperAccess.ExecuteSql(str);
}
else
{
// 验证重名
int haveFlower = Convert.ToInt32(DBHelperAccess.GetSingle("select count(1) from flower where mc='" + mc + "' and lx=" + lx));
if (haveFlower > 0)
{
Response.Write("<script>alert('该鲜花名已存在,不能添加!')</script>");
return;
}
tip = "添加";
str = "insert into flower(lx,zl,mc,gg,tp,tj) values(" + lx + "," + zl + ",'" + mc + "','" + gg + "','" + tp + "'," + ck_tj.Checked + ")";
i = DBHelperAccess.ExecuteSql(str);
if (i > 0)
{
/* 给每日报价添加这个花类 */
DataSet fidDs = DBHelperAccess.GetList("select max(id) as fid from flower where mc='" + mc + "'");
if (fidDs != null && fidDs.Tables.Count > 0)
{
string fid = fidDs.Tables[0].Rows[0]["fid"].ToString();
DBHelperAccess.ExecuteSql("insert into price(flowerid,priceInfoId) select " + fid + ",id from priceInfo");
}
}
}
if (i > 0)
{
Response.Write("<script>alert('" + tip + "成功!')</script>");
Response.Redirect("flowerMange.aspx");
}
else
{
Response.Write("<script>alert('" + tip + "失败!')</script>");
}
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="upload"></param>
/// <returns></returns>
private string UpImg ()
{
string tp = img_tp.PostedFile.FileName; // 图片
if (tp.Trim() == "")
{
return "";
}
else
{
try
{
FileInfo fi = new FileInfo(tp);
string[] upImgType = new string[] { ".jpg", ".jpeg", ".gif", ".bmp", ".png" }; // 允许上传图片的类型
if (upImgType.Count(t => t.ToLower() == fi.Extension.ToLower()) < 1)
{
Response.Write("<script>alert('不能上传该格式的图片!')</script>");
return null;
}
// 获取扩展名
string aLastName = fi.Extension;
string guid = Guid.NewGuid().ToString().Replace("-", "") + aLastName;
string savePath = Server.MapPath("~\\img"); //图片保存的文件夹
img_tp.PostedFile.SaveAs(savePath + "\\" + guid);//保存路径
//this.Image1.ImageUrl = "~\\excel" + "\\" + name;//显示图片
return guid;
}
catch
{
Response.Write("<script>alert('图片上传失败!')</script>");
return null;
}
}
}
protected void drp1_TextChanged (object sender, EventArgs e)
{
BindDrp2();
}
}