单击添加
点击编辑
单击选择
单击删除
1 <asp:Panel ID="Panel1" runat="server" Width="898px" Height="370px"
2 ScrollBars="Both">
3 <asp:GridView ID="GridView1" runat="server" Height="133px" Width="100%"
4 AutoGenerateColumns="False" DataKeyNames="id号"
5 OnRowEditing="GridView1_RowEditing" BackColor="White" BorderColor="White"
6 BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
7 CellSpacing="1" GridLines="None" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
8 OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting"
9 AllowPaging="True" >
10 <Columns>
11 <asp:BoundField DataField="ID号" HeaderText="ID号" />
12 <asp:BoundField DataField="名称" HeaderText="名称" />
13 <asp:BoundField DataField="编号" HeaderText="编号" />
14 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
15 <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
16 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
17 </Columns>
18 <PagerTemplate>
19 <div style="float:left;">
20 <asp:LinkButton ID="lb_first" runat="server" OnClick="lb_first_Click" ForeColor="#666666">首页</asp:LinkButton>
21 <asp:LinkButton ID="lb_up" runat="server" OnCommand="lb_up_Command" ForeColor="#666666">上一页</asp:LinkButton>
22 <asp:LinkButton ID="lb_down" runat="server" OnCommand="lb_down_Command" ForeColor="#666666">下一页</asp:LinkButton>
23 <asp:LinkButton ID="lb_end" runat="server" OnClick="lb_end_Click" ForeColor="#666666">尾页</asp:LinkButton>
24 页次:<asp:Label ID="labPage" runat="server" Text='<%# setPageIndex() %>'></asp:Label>/
25 <asp:Label ID="labPageCount" runat="server" Text='<%# setPageCount() %>'></asp:Label>页
26 共有 <asp:Label ID="labCount" runat="server" Text='<%# setRows()%>'></asp:Label>条信息,每页
27 <asp:Label ID="labPageSize" runat="server" Text='<%# setPageSize() %>'></asp:Label>条
28 </div>
29 </PagerTemplate>
30 <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
31 <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
32 <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
33 <SelectedRowStyle BackColor="#000000" Font-Bold="True" ForeColor="Red" />
34 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
35 </asp:GridView>
36 </asp:Panel>
37 <div style="height: 30px; 889px; text-align: center">
38 <div id="bt7">
39 <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
40 </div>
41 <div id="bt8">
42 <asp:Label ID="Label2" runat="server" Text=" "></asp:Label>
43 </div>
44 <div id="bt3" align="right">
45 <asp:Button ID="bt_new" runat="server" Height="25px" Text="添加"
46 Width="82px" onclick="bt_new_Click" UseSubmitBehavior="False" />
47 </div>
48 </div>
代码
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using Device.DAO;
14 using System.Data.Sql;
15 using System.Data.SqlClient;
16
17 namespace Device
18 {
19 public partial class SheBeiZhuangTaiWeiHu : System.Web.UI.Page
20 {
21 DataSet ds_sbztwh;//设备状态维护
22 SqlDataAdapter dat_sbztwh = new SqlDataAdapter();
23 public static DataTable dt_sbztk;
24
25 public static int rows_count = 0;
26
27 DBClass dbclass = new DBClass();
28
29 #region[事件] [Page_Load事件]
30 protected void Page_Load(object sender, EventArgs e)
31 {
32 if (!IsPostBack)
33 {
34 ds_sbztwh = new DataSet();
35 string sql_yjyb = string.Format("select * from 设备状态库 order by ID号 asc");
36 ds_sbztwh = dbclass.get_data(sql_yjyb, dat_sbztwh);
37 dt_sbztk = ds_sbztwh.Tables[0];
38 this.GridView1.DataSource = dt_sbztk;
39 this.GridView1.DataBind();
40 ViewState["PageIndex"] = 0;
41 }
42 }
43 #endregion
44
45
46 #region[事件] [添加Linkbutton]
47 protected void lb_add_Click(object sender, EventArgs e)
48 {
49 this.GridView1.ShowFooter = true;
50 }
51 #endregion
52
53 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
54 {
55 GridViewRow row = GridView1.Rows[e.RowIndex];
56 string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();
57 string sql_del = string.Format("delete from 设备状态库 where ID号="+row.Cells[0].Text.ToString().Trim()+"");
58 dbclass.save_data(sql_del);
59 dt_sbztk.Rows[e.RowIndex].Delete();
60 Response.Write("<script type='text/javascript'>window.alert('删除成功')</script>"); DataBindings();
61 }
62
63 #region[事件] [添加按钮]
64 protected void bt_new_Click(object sender, EventArgs e)
65 {
66 string DEGREE_idstr = "select ident_current('设备状态库')+1";
67 int bh = dbclass.get_id(DEGREE_idstr);
68 string sql_str = "insert into 设备状态库(名称,编号) values( ' ',' ')";
69 dbclass.save_data(sql_str);
70 DataRow dr = dt_sbztk.NewRow();
71 dr["id号"] = bh.ToString();
72 dt_sbztk.Rows.Add(dr);
73 GridView1.DataSource = dt_sbztk;
74 this.GridView1.DataBind();
75 AllowPage(this.GridView1.PageCount - 1);
76 }
77 #endregion
78
79 public void DataBindings()
80 {
81 ds_sbztwh = new DataSet();
82 string sql_yjyb = string.Format("select * from 设备状态库 order by ID号 asc");
83 ds_sbztwh = dbclass.get_data(sql_yjyb, dat_sbztwh);
84 dt_sbztk = ds_sbztwh.Tables[0];
85 this.GridView1.DataSource = dt_sbztk;
86 this.GridView1.DataBind();
87 }
88
89 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
90 {
91 this.GridView1.EditIndex = -1;
92 DataBindings();
93 }
94
95 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
96 {
97 if (e.Row.RowType == DataControlRowType.DataRow)
98 {
99 if(e.Row.RowState==DataControlRowState.Normal|| e.Row.RowState==DataControlRowState.Alternate)
100 {
101
102 ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("OnClick", "javascript: return confirm('你确认要删除\"" + e.Row.Cells[0].Text.Trim() + "\"吗?')");
103 //鼠标经过时,行背景色变
104 e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#E6F5FA'");
105 //鼠标移出时,行背景色变
106 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DEDFDE'");
107 }
108 }
109 }
110
111 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
112 {
113 GridView1.EditIndex = e.NewEditIndex;
114 DataBindings();
115 }
116
117 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
118 {
119 GridViewRow row = GridView1.Rows[e.RowIndex];
120 int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
121
122 string name = ((TextBox)(row.Cells[1].Controls[0])).Text.Trim();
123 string score = ((TextBox)(row.Cells[2].Controls[0])).Text.Trim();
124 string sql_up = string.Format("update 设备状态库 set 名称= '" + name + "', 编号='" + score + "' where ID号=" + ID + "");
125 dbclass.save_data(sql_up);
126 GridView1.EditIndex = -1;
127
128
129 Response.Write("<script type='text/javascript'>window.alert('修改成功')</script>"); DataBindings();
130 DataBind();
131 }
132
133 #region[事件 首页]
134 protected void lb_first_Click(object sender, EventArgs e)
135 {
136 AllowPage(0);
137 }
138 #endregion
139
140 #region[事件 上一页]
141 protected void lb_up_Command(object sender, CommandEventArgs e)
142 {
143 PageIndex--;
144 AllowPage(PageIndex);
145 }
146 #endregion
147
148 #region[事件 下一页]
149 protected void lb_down_Command(object sender, CommandEventArgs e)
150 {
151 PageIndex++;
152 AllowPage(PageIndex);
153 }
154 #endregion
155
156 #region[事件 尾页]
157 protected void lb_end_Click(object sender, EventArgs e)
158 {
159 AllowPage(this.GridView1.PageCount - 1);
160 }
161 #endregion
162
163 #region[方法 翻页]
164 protected void AllowPage(int pageIndex)
165 {
166 int max = this.GridView1.PageCount - 1;
167 if (pageIndex > max)
168 {
169 this.GridView1.PageIndex = max;
170 ViewState["PageIndex"] = max;
171 }
172 else
173 {
174 this.GridView1.PageIndex = pageIndex;
175 ViewState["PageIndex"] = pageIndex;
176 }
177 DataBindings();
178 }
179 #endregion
180
181 #region[获取页码]
182 protected int PageIndex
183 {
184 set
185 {
186 if (value < 0)
187 {
188 ViewState["PageIndex"] = 0;
189 }
190 else
191 {
192 ViewState["PageIndex"] = value;
193 }
194 }
195 get
196 {
197 return int.Parse(ViewState["PageIndex"].ToString());
198 }
199 }
200 #endregion
201
202 #region[方法 获取行号]
203 public string vsRows
204 {
205 get
206 {
207 if (EnableViewState)
208 {
209 object obj = ViewState["vsRows"];
210 if (obj != null)
211 return (string)obj;
212 else
213 return string.Empty;
214 }
215 else
216 return string.Empty;
217 }
218 set
219 {
220 if (EnableViewState)
221 ViewState["vsRows"] = value;
222 }
223 }
224 #endregion
225
226 #region[方法 setRows]
227 protected string setRows()
228 {
229 if (!string.IsNullOrEmpty(vsRows))
230 {
231 return vsRows;
232 }
233 return "0";
234 }
235 #endregion
236
237 #region[方法 设置页码]
238 protected string setPageIndex()
239 {
240 return (this.GridView1.PageIndex + 1).ToString();
241 }
242 #endregion
243
244 #region[方法 每页的条数]
245 protected string setPageCount()
246 {
247 return this.GridView1.PageCount.ToString();
248 }
249 #endregion
250
251 #region[方法 setPageSize]
252 protected string setPageSize()
253 {
254 return this.GridView1.PageSize.ToString();
255 }
256 #endregion
257 }
258 }
259
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Data.SqlClient;
6 using System.Data;
7
8 namespace Device.DAO
9 {
10 public class DBClass
11 {
12 string constr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
13
14 public string[] get_type(string sql) //取值的字段名一定要放在第一位。
15 {
16 string[] arr=null;
17 string arrstr=null;
18 try
19 {
20 SqlConnection sqlcon = new SqlConnection(constr);
21 sqlcon.Open();
22 SqlDataAdapter sqldba = new SqlDataAdapter(sql, sqlcon);
23 DataSet ds = new DataSet();
24 sqldba.Fill(ds);
25 sqlcon.Close();
26 if ((ds != null) && (ds.Tables[0] != null) && (ds.Tables[0].Rows.Count > 0))
27 {
28 for(int i=0;i<=ds.Tables[0].Rows.Count-1;i++)
29 {
30 arrstr += ds.Tables[0].Rows[i].ItemArray[0].ToString() +";";
31 }
32 arr = arrstr.Split(';');
33 }
34 return arr;
35 }
36 catch
37 {
38 return arr;
39 }
40
41 }
42
43 public int get_id(string sql)
44 {
45 int id = 0;
46 SqlConnection sqlcon = new SqlConnection(constr);
47 sqlcon.Open();
48 SqlDataAdapter sqldba = new SqlDataAdapter(sql, sqlcon);
49 DataSet ds = new DataSet();
50 sqldba.Fill(ds);
51 sqlcon.Close();
52 if((ds!=null)&&(ds.Tables[0]!=null)&&(ds.Tables[0].Rows.Count>0))
53 id=Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString());
54 return id;
55 }
56
57 public DataSet get_data(string sql,SqlDataAdapter sqldba)
58 {
59 SqlConnection sqlcon = new SqlConnection(constr);
60 sqlcon.Open();
61 sqldba = new SqlDataAdapter(sql, sqlcon);
62 DataSet ds = new DataSet();
63 sqldba.Fill(ds);
64 sqlcon.Close();
65 return ds;
66 }
67
68 public bool update_data(DataSet ds,SqlDataAdapter sqldba)
69 {
70 bool bo = false;
71 try
72 {
73 SqlConnection sqlcon = new SqlConnection(constr);
74 sqlcon.Open();
75 sqldba.Update(ds);
76 sqlcon.Close();
77 bo = true;
78 }
79 catch
80 {
81 bo = false;
82 }
83 return bo;
84 }
85
86 public bool save_data(string comstr)
87 {
88 bool bo = false;
89 try
90 {
91 SqlConnection sqlcon = new SqlConnection(constr);
92 sqlcon.Open();
93 SqlCommand sqlcom = new SqlCommand();
94 sqlcom.Connection = sqlcon;
95 sqlcom.CommandText = comstr;
96 sqlcom.ExecuteNonQuery();
97 bo = true;
98 sqlcon.Close();
99 }
100 catch
101 {
102 bo = false;
103 }
104 return bo;
105 }
106 }
107 }
108