-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowerMange.aspx.cs
240 lines (214 loc) · 6.65 KB
/
flowerMange.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class flowerMange : System.Web.UI.Page
{
public class FlowerList
{
public int Id { get; set; }
public string mc { get; set; }
public string tp { get; set; }
public string gg { get; set; }
}
/// <summary>
/// 绑定下拉列表
/// </summary>
private void BindDrp1 ()
{
DataSet ds = DBHelperAccess.GetList("select id,lxmc from type where lx='0'");
drp1.DataValueField = "id";
drp1.DataTextField = "lxmc";
drp1.DataSource = InsertTotal(ds.Tables[0]);
drp1.DataBind();
}
private void BindDrp2 ()
{
string value = drp1.SelectedValue;
if (value != "0")
{
DataSet ds = DBHelperAccess.GetList("select id,lxmc from type where lx='" + value + "' and zl='1'");
drp2.DataValueField = "id";
drp2.DataTextField = "lxmc";
drp2.DataSource = InsertTotal(ds.Tables[0]);
drp2.DataBind();
}
}
/// <summary>
/// 添加全部项
/// </summary>
/// <returns></returns>
private DataTable InsertTotal (DataTable dt)
{
DataRow dr = dt.NewRow();
dr["id"] = "0";
dr["lxmc"] = "全部";
dt.Rows.InsertAt(dr, 0);
return dt;
}
private void Query ()
{
string where = " 1=1 ";
if (drp1.SelectedValue != "0")
{
where += " and lx=" + drp1.SelectedValue;
}
if (drp2.SelectedValue != "0")
{
where += " and zl=" + drp2.SelectedValue;
}
int totalCount = (int)DBHelperAccess.GetList("select count(1) as num from flower where " + where).Tables[0].Rows[0]["num"]; //获得总记录数
int pageSize = 10;//显示数据条数
int curPage = Convert.ToInt32(lblCurrentPage.Text.Trim());//这是页面显示的第几页那个几
int tmpInt = totalCount / pageSize;
int pages = totalCount % pageSize == 0 ? tmpInt : (tmpInt + 1);//总页数
lblCount.Text = totalCount.ToString();
lblPage.Text = pages.ToString();
next.Enabled = true;
up.Enabled = true;
first.Enabled = true;
last.Enabled = true;
if (curPage == 1)
{
up.Enabled = false;
first.Enabled = false;
}
if (curPage == pages)
{
next.Enabled = false;
last.Enabled = false;
}
string sql = "select * from flower f where " + where+ " order by f.lx,f.zl,f.id";
DataSet ds = DBHelperAccess.GetList(sql);
int minRow = pageSize * (curPage - 1);
int maxRow = pageSize * curPage ;
Repeater1.DataSource = ds.Tables.Count == 0 ? null : GetCurPageDate(ds.Tables[0], minRow, maxRow);
Repeater1.DataBind();
}
private DataTable GetCurPageDate (DataTable dt, int minRow, int maxRow)
{
List<FlowerList> flowerLists = new List<FlowerList>();
int count = dt.Rows.Count > maxRow ? maxRow : dt.Rows.Count;
for (int i = minRow; i < count; i++)
{
FlowerList flowerList = new FlowerList() {
Id = (int)dt.Rows[i]["id"],
mc = dt.Rows[i]["mc"].ToString(),
tp = dt.Rows[i]["tp"].ToString(),
gg = dt.Rows[i]["gg"].ToString(),
};
flowerLists.Add(flowerList);
}
return FunctionModel.ToDataTable(flowerLists);
}
protected void Page_Load (object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Response.Write("<script>alert('登录超时,请从新登录!')</script>");
Response.Redirect("login.aspx");
return;
}
if (!IsPostBack)
{
lblCurrentPage.Text = "1"; // 初始化页数
BindDrp1();
BindDrp2();
Query();
}
}
protected void btn_query_Click (object sender, EventArgs e)
{
lblCurrentPage.Text = "1";
Query();
}
#region 第一页
protected void first_Click (object sender, EventArgs e)
{
lblCurrentPage.Text = "1";
Query();
}
#endregion
#region 下一页
protected void next_Click (object sender, EventArgs e)
{
int page = int.Parse(lblCurrentPage.Text.Trim());
lblCurrentPage.Text = (page + 1).ToString();
Query();
}
#endregion
#region 上一页
protected void up_Click (object sender, EventArgs e)
{
int page = int.Parse(lblCurrentPage.Text.Trim());
lblCurrentPage.Text = (page - 1).ToString();
Query();
}
#endregion
#region 最后页
protected void last_Click (object sender, EventArgs e)
{
int page = int.Parse(lblPage.Text.Trim());
lblCurrentPage.Text = page.ToString();
Query();
}
#endregion
#region 跳转到第n页
protected void btnGo_Click (object sender, EventArgs e)
{
//转到第N页代码
if (!Regex.IsMatch(txtPage.Text.Trim(), @"^([1-9]\d*)$"))//验证输入数字是否合法
{
txtPage.Text = "1";
}
int curPage = int.Parse(txtPage.Text.Trim());
int allPage = int.Parse(lblPage.Text.Trim());
if (curPage > allPage)
curPage = allPage;
txtPage.Text = lblCurrentPage.Text = curPage.ToString();
Query();
}
#endregion
#region Repeater1_ItemCommand事件
protected void Repeater1_ItemCommand (object source, RepeaterCommandEventArgs e)
{
string id = e.CommandArgument.ToString();
if (e.CommandName == "Edit" && !String.IsNullOrEmpty(id))
{
Response.Redirect("addFlower.aspx?id=" + id);
}
else if (e.CommandName == "Delete" && !String.IsNullOrEmpty(id))
{
int i = DBHelperAccess.ExecuteSql("delete from flower where id=" + id);
DBHelperAccess.ExecuteSql("delete from price where flowerid=" + id); // 删除相关数据
if (i == 0)
{
Response.Write("<script>alert('删除失败!')</script>");
}
else
{
Query();
}
}
else
{
return;
}
}
#endregion
protected void drp1_TextChanged (object sender, EventArgs e)
{
BindDrp2();
Query();
}
protected void drp2_TextChanged (object sender, EventArgs e)
{
Query();
}
}