ASP.NET中的DataGrid控件示例

ASP.NET中的DataGrid控件示例 Powered By:记得忘记

关于DataGrid 的几点简单应用:
1、有关checkbox的几个功能:全部选种、取消选种、没选中操作的检测等,这几个均使用js脚本实现。
2、根据主键关键字进行查询,如果条件输入为空,则检索所有数据
3、显示页面状态,第几页总共几页
4、DataGrid 分页、根据输入的数字跳转到指定页
5、DataGrid的正反双向排序
6、DataGrid的删除、根据主键进行删除
7、DataGrid的编辑、点击后弹出更新、取消按钮
8、插入数据操作、通用BLL层实现
9、DataGrid 根据复选框删除对应记录

ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifnamespace WebUI.Modules.BaseData
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    using System;
  4InBlock.gif    using System.Data;
  5InBlock.gif    using System.Drawing;
  6InBlock.gif    using System.Web;
  7InBlock.gif    using System.Web.UI.WebControls;
  8InBlock.gif    using System.Web.UI.HtmlControls;
  9InBlock.gif    using BLL.Modules.BaseData;    
 10InBlock.gif    using Common.Modules.BaseData;
 11InBlock.gif    using System.IO;
 12InBlock.gif
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 14InBlock.gif    ///        SugarcaneClassInfo 的摘要说明。
 15ExpandedSubBlockEnd.gif    /// </summary>

 16InBlock.gif    public class SugarcaneClassInfo : System.Web.UI.UserControl
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18InBlock.gif        protected System.Web.UI.WebControls.DataGrid DataGrid1;
 19InBlock.gif        protected System.Web.UI.WebControls.TextBox txtClassID;
 20InBlock.gif        protected System.Web.UI.WebControls.ImageButton ibtnAdd;
 21InBlock.gif        protected System.Web.UI.WebControls.LinkButton btnLast;
 22InBlock.gif        protected System.Web.UI.WebControls.LinkButton btnNext;
 23InBlock.gif        protected System.Web.UI.WebControls.LinkButton btnPrev;
 24InBlock.gif        protected System.Web.UI.WebControls.LinkButton btnFirst;
 25InBlock.gif        protected System.Web.UI.WebControls.Label lblCurrentIndex;
 26InBlock.gif        protected System.Web.UI.WebControls.Label lblPageCount;
 27InBlock.gif        protected System.Web.UI.WebControls.TextBox go;
 28InBlock.gif        protected System.Web.UI.WebControls.Label Label1;
 29InBlock.gif        TSugarcaneClassBiz tSugarcaneClass = new TSugarcaneClassBiz();
 30InBlock.gif        protected System.Web.UI.WebControls.ImageButton ibtnSearch;
 31InBlock.gif        protected System.Web.UI.WebControls.TextBox txtClassName;
 32InBlock.gif        protected System.Web.UI.WebControls.TextBox txtPrice;
 33InBlock.gif        protected System.Web.UI.WebControls.TextBox txtRemark;
 34InBlock.gif        protected System.Web.UI.WebControls.ImageButton ibtnCancel;
 35InBlock.gif        protected System.Web.UI.WebControls.ImageButton ibtnDel;
 36InBlock.gif        TSugarcaneClassData tSugarcaneClassData = new TSugarcaneClassData();
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 38InBlock.gif        /// 页面加载初始化工作
 39InBlock.gif        /// Powered By:CHENQP
 40InBlock.gif        /// </summary>
 41InBlock.gif        /// <param name="sender"></param>
 42ExpandedSubBlockEnd.gif        /// <param name="e"></param>

 43InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 45InBlock.gif            // 在此处放置用户代码以初始化页面
 46InBlock.gif            this.ibtnDel.Attributes.Add("onclick","javascript:return DelRec();");
 47InBlock.gif            btnFirst.Text = "首页";
 48InBlock.gif            btnPrev.Text = "前一页";
 49InBlock.gif            btnNext.Text = "下一页";
 50InBlock.gif            btnLast.Text = "尾页";
 51InBlock.gif            if(this.DataGrid1.Attributes["SortExpression"]==null
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                this.DataGrid1.Attributes["SortExpression"]="ClassID"
 54InBlock.gif                this.DataGrid1.Attributes["SortDirection"]="ASC"
 55ExpandedSubBlockEnd.gif            }

 56InBlock.gif            SetBind(this.DataSource());
 57InBlock.gif            ShowStats();
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 61InBlock.gif        /// 得到数据列表、通过BLL层返回数据
 62InBlock.gif        /// Powered By:CHENQP
 63InBlock.gif        /// </summary>
 64ExpandedSubBlockEnd.gif        /// <returns></returns>

 65InBlock.gif        protected DataView DataSource()
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            DataTable dt = tSugarcaneClass.SelectAll();
 68InBlock.gif            return dt.DefaultView;
 69ExpandedSubBlockEnd.gif        }

 70InBlock.gif
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 72InBlock.gif        /// 绑定数据源,同时考虑排序条件
 73InBlock.gif        /// Powered By:CHENQP
 74InBlock.gif        /// </summary>
 75ExpandedSubBlockEnd.gif        /// <param name="dv"></param>

 76InBlock.gif        private void SetBind(DataView dv)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78InBlock.gif            string SortExpression=this.DataGrid1.Attributes["SortExpression"];
 79InBlock.gif            string SortDirection=this.DataGrid1.Attributes["SortDirection"];
 80InBlock.gif            dv.Sort=SortExpression+" "+SortDirection; //指定视图的排序方式;
 81InBlock.gif            try
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                this.DataGrid1.DataSource=dv;
 84InBlock.gif                this.DataGrid1.DataBind();
 85ExpandedSubBlockEnd.gif            }

 86InBlock.gif            catch
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 88InBlock.gif                DataGrid1.CurrentPageIndex = 0;
 89InBlock.gif                this.DataGrid1.DataSource=dv;
 90InBlock.gif                this.DataGrid1.DataBind();
 91ExpandedSubBlockEnd.gif            }

 92InBlock.gif            
 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 96InBlock.gif        override protected void OnInit(EventArgs e)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            //
 99InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
100InBlock.gif            //
101InBlock.gif            InitializeComponent();
102InBlock.gif            base.OnInit(e);
103ExpandedSubBlockEnd.gif        }

104InBlock.gif        
105ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
106InBlock.gif        ///        设计器支持所需的方法 - 不要使用代码编辑器
107InBlock.gif        ///        修改此方法的内容。
108ExpandedSubBlockEnd.gif        /// </summary>

109InBlock.gif        private void InitializeComponent()
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            this.ibtnSearch.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnSearch_Click);
112InBlock.gif            this.ibtnAdd.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnAdd_Click);
113InBlock.gif            this.ibtnDel.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnDel_Click);
114InBlock.gif            this.ibtnCancel.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnCancel_Click);
115InBlock.gif            this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
116InBlock.gif            this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
117InBlock.gif            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
118InBlock.gif            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
119InBlock.gif            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
120InBlock.gif            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
121InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
122InBlock.gif
123ExpandedSubBlockEnd.gif        }

124ExpandedSubBlockEnd.gif        #endregion

125ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
126InBlock.gif        /// 选中DataGridJ时改变颜色,通过JS实现
127InBlock.gif        /// Powered By:CHENQP
128InBlock.gif        /// </summary>
129InBlock.gif        /// <param name="sender"></param>
130ExpandedSubBlockEnd.gif        /// <param name="e"></param>

131InBlock.gif        private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
132ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
133InBlock.gif            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
134ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
135InBlock.gif                e.Item.Attributes.Add("id","row"+e.Item.ItemIndex);
136InBlock.gif                e.Item.Attributes.Add("onclick","javascript:return SelectRow("+e.Item.ItemIndex+");");
137ExpandedSubBlockEnd.gif            }

138ExpandedSubBlockEnd.gif        }

139InBlock.gif
140ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
141InBlock.gif        /// 根据主键关键字进行查询,如果条件输入为空,则检索所有数据
142InBlock.gif        /// Powered By:CHENQP
143InBlock.gif        /// </summary>
144InBlock.gif        /// <param name="sender"></param>
145ExpandedSubBlockEnd.gif        /// <param name="e"></param>

146InBlock.gif        private void ibtnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
148InBlock.gif            string strClassID = txtClassID.Text;
149InBlock.gif            strClassID = strClassID.ToString().Trim();
150InBlock.gif            if (strClassID.Length == 0)
151ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
152ExpandedSubBlockEnd.gif            }

153InBlock.gif            else
154ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
155InBlock.gif                DataTable dt = tSugarcaneClass.SelectByClassID(strClassID);
156InBlock.gif                SetBind(dt.DefaultView);
157ExpandedSubBlockEnd.gif            }

158ExpandedSubBlockEnd.gif        }

159InBlock.gif
160ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
161InBlock.gif        /// 显示页面状态,第几页总共几页
162InBlock.gif        /// Powered By:CHENQP
163ExpandedSubBlockEnd.gif        /// </summary>

164InBlock.gif        private void ShowStats()
165ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
166InBlock.gif            lblCurrentIndex.Text = "第 " + (this.DataGrid1.CurrentPageIndex + 1).ToString() + " 页";
167InBlock.gif            lblPageCount.Text = "总共 " + this.DataGrid1.PageCount.ToString() + " 页";
168ExpandedSubBlockEnd.gif        }

169InBlock.gif
170ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
171InBlock.gif        /// DataGrid 分页
172InBlock.gif        /// Powered By:CHENQP
173InBlock.gif        /// </summary>
174InBlock.gif        /// <param name="sender"></param>
175ExpandedSubBlockEnd.gif        /// <param name="e"></param>

176InBlock.gif        public void PagerButtonClick(object sender, EventArgs e)
177ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
178InBlock.gif            string arg = ((LinkButton)sender).CommandArgument.ToString();
179InBlock.gif            switch(arg)
180ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
181InBlock.gif                case "next":
182InBlock.gif                    if (this.DataGrid1.CurrentPageIndex < (this.DataGrid1.PageCount - 1))
183ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
184InBlock.gif                        this.DataGrid1.CurrentPageIndex += 1;
185ExpandedSubBlockEnd.gif                    }

186InBlock.gif                    break;
187InBlock.gif                case "prev":
188InBlock.gif                    if (this.DataGrid1.CurrentPageIndex > 0)
189ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
190InBlock.gif                        this.DataGrid1.CurrentPageIndex -= 1;
191ExpandedSubBlockEnd.gif                    }

192InBlock.gif                    break;
193InBlock.gif                case "last":
194InBlock.gif                    this.DataGrid1.CurrentPageIndex = (this.DataGrid1.PageCount - 1);
195InBlock.gif                    break;
196InBlock.gif                default:
197InBlock.gif                    this.DataGrid1.CurrentPageIndex = System.Convert.ToInt32(arg);
198InBlock.gif                    break;
199ExpandedSubBlockEnd.gif            }

200InBlock.gif            SetBind(this.DataSource());
201InBlock.gif            ShowStats();
202ExpandedSubBlockEnd.gif        }

203InBlock.gif
204ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
205InBlock.gif        /// 判断输入的跳转页是否为数字
206InBlock.gif        /// Powered By:CHENQP
207InBlock.gif        /// </summary>
208InBlock.gif        /// <param name="oText"></param>
209ExpandedSubBlockEnd.gif        /// <returns></returns>

210InBlock.gif        private bool IsNumberic(string oText)
211ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
212InBlock.gif            try
213ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
214InBlock.gif                int var1=Convert.ToInt32 (oText);
215InBlock.gif                return true;
216ExpandedSubBlockEnd.gif            }

217InBlock.gif            catch
218ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
219InBlock.gif                return false;
220ExpandedSubBlockEnd.gif            }

221ExpandedSubBlockEnd.gif        }

222InBlock.gif
223ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
224InBlock.gif        /// 根据输入的数字跳转到指定页
225InBlock.gif        /// Powered By:CHENQP
226InBlock.gif        /// </summary>
227InBlock.gif        /// <param name="obj"></param>
228ExpandedSubBlockEnd.gif        /// <param name="e"></param>

229InBlock.gif        public void goClick(object obj,EventArgs e)
230ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
231InBlock.gif            if(go.Text.Trim()!=""&&this.IsNumberic(go.Text.Trim()))
232ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
233InBlock.gif                int index=Int32.Parse(go.Text.Trim())-1;
234InBlock.gif                if(index>=0 && index<this.DataGrid1.PageCount)
235ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
236InBlock.gif                    this.DataGrid1.CurrentPageIndex=index;
237ExpandedSubBlockEnd.gif                }

238InBlock.gif                SetBind(this.DataSource());
239InBlock.gif                ShowStats();
240ExpandedSubBlockEnd.gif            }

241InBlock.gif            else
242ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
243InBlock.gif                Response.Write("<script>alert(\'跳转不能为空且必须为数字\')</script>");
244ExpandedSubBlockEnd.gif            }

245ExpandedSubBlockEnd.gif        }

246InBlock.gif
247ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
248InBlock.gif        /// DataGrid的正反双向排序 
249InBlock.gif        /// Powered By:CHENQP
250InBlock.gif        /// </summary>
251InBlock.gif        /// <param name="source"></param>
252ExpandedSubBlockEnd.gif        /// <param name="e"></param>

253InBlock.gif        private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
254ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
255InBlock.gif            string SortExpression=e.SortExpression.ToString();  //获得当前排序表达式
256InBlock.gif            string SortDirection="ASC"//为排序方向变量赋初值
257InBlock.gif            if(SortExpression==this.DataGrid1.Attributes["SortExpression"])  //如果为当前排序列
258ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
259InBlock.gif                SortDirection=(this.DataGrid1.Attributes["SortDirection"].ToString()==SortDirection?"DESC":"ASC");     //获得下一次的排序状态
260InBlock.gif
261ExpandedSubBlockEnd.gif            }

262InBlock.gif            this.DataGrid1.Attributes["SortExpression"]=SortExpression;
263InBlock.gif            this.DataGrid1.Attributes["SortDirection"]=SortDirection;
264InBlock.gif            SetBind(this.DataSource());
265ExpandedSubBlockEnd.gif        }

266InBlock.gif
267ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
268InBlock.gif        /// DataGrid的删除、根据主键进行删除
269InBlock.gif        /// Powered By:CHENQP
270InBlock.gif        /// </summary>
271InBlock.gif        /// <param name="source"></param>
272ExpandedSubBlockEnd.gif        /// <param name="e"></param>

273InBlock.gif        private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
274ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
275InBlock.gif            string strClassID = e.Item.Cells[2].Text;
276InBlock.gif            strClassID = strClassID.Trim();
277InBlock.gif            if(strClassID=="")
278ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
279ExpandedSubBlockEnd.gif            }

280InBlock.gif            else
281ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{    
282InBlock.gif                try
283ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
284InBlock.gif                    if (tSugarcaneClass.Delete(strClassID))
285ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
286InBlock.gif                        BaseClass.MessageBox.Show(this.Page,"删除数据操作成功");
287ExpandedSubBlockEnd.gif                    }

288InBlock.gif                    else
289ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
290ExpandedSubBlockEnd.gif                    }

291ExpandedSubBlockEnd.gif                }

292InBlock.gif                catch(Exception ex)
293ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
294InBlock.gif                    throw ex;
295ExpandedSubBlockEnd.gif                }

296InBlock.gif        
297InBlock.gif                this.DataGrid1.EditItemIndex=-1;
298InBlock.gif                SetBind(this.DataSource());
299ExpandedSubBlockEnd.gif            }

300ExpandedSubBlockEnd.gif        }

301InBlock.gif
302ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
303InBlock.gif        /// DataGrid的编辑、点击后弹出更新、取消按钮
304InBlock.gif        /// Powered By:CHENQP
305InBlock.gif        /// </summary>
306InBlock.gif        /// <param name="source"></param>
307ExpandedSubBlockEnd.gif        /// <param name="e"></param>

308InBlock.gif        private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
309ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
310InBlock.gif            this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
311InBlock.gif            SetBind(this.DataSource());
312ExpandedSubBlockEnd.gif        }

313InBlock.gif
314ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
315InBlock.gif        /// DataGrid的编辑、取消按钮
316InBlock.gif        /// Powered By:CHENQP
317InBlock.gif        /// </summary>
318InBlock.gif        /// <param name="source"></param>
319ExpandedSubBlockEnd.gif        /// <param name="e"></param>

320InBlock.gif        private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
321ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
322InBlock.gif                
323InBlock.gif            this.DataGrid1.EditItemIndex = -1;
324InBlock.gif            SetBind(this.DataSource());
325ExpandedSubBlockEnd.gif        }

326InBlock.gif
327ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
328InBlock.gif        /// DataGrid的编辑、更新按钮、根据主键进行更新、主键不能更改
329InBlock.gif        /// Powered By:CHENQP
330InBlock.gif        /// </summary>
331InBlock.gif        /// <param name="source"></param>
332ExpandedSubBlockEnd.gif        /// <param name="e"></param>

333InBlock.gif        private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
334ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
335InBlock.gif            if(e.CommandName.ToString() == "Update" )
336ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
337InBlock.gif                string strClassID = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
338InBlock.gif                string strClassName = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
339InBlock.gif                string strPrice = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
340InBlock.gif                decimal decPrice = Decimal.Parse(strPrice); 
341InBlock.gif                string strRemark = ((TextBox)e.Item.Cells[5].Controls[0]).Text; 
342InBlock.gif                tSugarcaneClassData.ClassID=strClassID.Trim();
343InBlock.gif                tSugarcaneClassData.ClassName=strClassName.Trim();
344InBlock.gif                tSugarcaneClassData.Price=decPrice;
345InBlock.gif                tSugarcaneClassData.Type="";
346InBlock.gif                tSugarcaneClassData.Remark=strRemark.Trim();
347InBlock.gif                tSugarcaneClassData.PonderType="";
348InBlock.gif
349InBlock.gif                tSugarcaneClass.Update(tSugarcaneClassData);
350InBlock.gif                this.DataGrid1.EditItemIndex = -1;
351InBlock.gif                SetBind(this.DataSource());
352ExpandedSubBlockEnd.gif            }
            
353ExpandedSubBlockEnd.gif        }

354InBlock.gif
355ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
356InBlock.gif        /// 页面取消按钮:把相关文本框的值清空
357InBlock.gif        /// Powered by:CHENQP
358InBlock.gif        /// </summary>
359InBlock.gif        /// <param name="sender"></param>
360ExpandedSubBlockEnd.gif        /// <param name="e"></param>

361InBlock.gif        private void ibtnCancel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
362ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
363InBlock.gif            txtClassID.Text="";
364InBlock.gif            txtClassName.Text="";
365InBlock.gif            txtPrice.Text="";
366InBlock.gif            txtRemark.Text="";                    
367ExpandedSubBlockEnd.gif        }

368InBlock.gif
369ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
370InBlock.gif        /// 插入数据操作、通用BLL层实现
371InBlock.gif        /// Powered By:CHENQP
372InBlock.gif        /// </summary>
373InBlock.gif        /// <param name="sender"></param>
374ExpandedSubBlockEnd.gif        /// <param name="e"></param>

375InBlock.gif        private void ibtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
376ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
377InBlock.gif            string strClassID = txtClassID.Text.ToString().Trim();
378InBlock.gif            string strClassName = txtClassName.Text.ToString().Trim();
379InBlock.gif            string strPrice = txtPrice.Text.ToString().Trim();
380InBlock.gif            decimal decPrice = Decimal.Parse(strPrice); //或者Convert.ToDecimal(strPrice)两种写法
381InBlock.gif            string strRemark = txtRemark.Text.ToString().Trim();
382InBlock.gif
383InBlock.gif            tSugarcaneClassData.ClassID=strClassID.Trim();
384InBlock.gif            tSugarcaneClassData.ClassName=strClassName.Trim();
385InBlock.gif            tSugarcaneClassData.Price=decPrice;
386InBlock.gif            tSugarcaneClassData.Type="";
387InBlock.gif            tSugarcaneClassData.Remark=strRemark.Trim();
388InBlock.gif            tSugarcaneClassData.PonderType="";
389InBlock.gif            if (tSugarcaneClass.Insert(tSugarcaneClassData))
390ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
391InBlock.gif                BaseClass.MessageBox.Show(this.Page,"增加数据操作成功");
392ExpandedSubBlockEnd.gif            }

393InBlock.gif            else
394ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
395ExpandedSubBlockEnd.gif            }
;
396InBlock.gif            SetBind(this.DataSource());
397ExpandedSubBlockEnd.gif        }

398InBlock.gif
399ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
400InBlock.gif        /// DataGrid 根据复选框删除对应记录
401InBlock.gif        /// Powered By:CHENQP
402InBlock.gif        /// </summary>
403InBlock.gif        /// <param name="sender"></param>
404ExpandedSubBlockEnd.gif        /// <param name="e"></param>

405InBlock.gif        private void ibtnDel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
406ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
407InBlock.gif            string strClassID="";
408InBlock.gif            for(int i=0;i<DataGrid1.Items.Count;i++)
409ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
410InBlock.gif                CheckBox h;
411InBlock.gif                h=(CheckBox)DataGrid1.Items[i].Cells[0].Controls[1];
412InBlock.gif                if(h.Checked == true)
413ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
414InBlock.gif                    //取得已选取的主键
415InBlock.gif                    strClassID = DataGrid1.Items[i].Cells[2].Text.ToString().Trim();
416InBlock.gif                    try
417ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
418InBlock.gif                        if (tSugarcaneClass.Delete(strClassID))
419ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{                                    
420ExpandedSubBlockEnd.gif                        }

421InBlock.gif                        else
422ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
423ExpandedSubBlockEnd.gif                        }

424ExpandedSubBlockEnd.gif                    }

425InBlock.gif                    catch(Exception ex)
426ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
427InBlock.gif                        throw ex;
428ExpandedSubBlockEnd.gif                    }

429ExpandedSubBlockEnd.gif                }

430ExpandedSubBlockEnd.gif            }

431InBlock.gif            BaseClass.MessageBox.Show(this.Page,"删除数据操作成功");
432InBlock.gif            SetBind(this.DataSource());
433ExpandedSubBlockEnd.gif        }

434ExpandedSubBlockEnd.gif    }

435ExpandedBlockEnd.gif}

436None.gif

SugarcaneClassInfo.ascx:
  1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Register TagPrefix="cc1" Namespace="BaseComponent" Assembly="BaseComponent"  %>
  2 ExpandedBlockStart.gifContractedBlock.gif <% dot.gif @ Control Language="c#" AutoEventWireup="false" Codebehind="SugarcaneClassInfo.ascx.cs" Inherits="WebUI.Modules.BaseData.SugarcaneClassInfo" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
  3 ExpandedBlockStart.gifContractedBlock.gif < script > dot.gif
  4InBlock.gif        //DataGrid单击行时改变颜色
  5InBlock.gif        var oldrow;
  6InBlock.gif        var newColor='#BDBD00';
  7InBlock.gif        var oldColor;
  8InBlock.gif
  9InBlock.gif        function SelectRow(rowno)
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 11InBlock.gif        if (oldrow == null)
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 13InBlock.gif        oldColor = document.all('row'+rowno).style.backgroundColor;
 14InBlock.gif        document.all('row'+rowno).style.backgroundColor = newColor;
 15ExpandedSubBlockEnd.gif        }

 16InBlock.gif        else
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif        oldrow.style.backgroundColor = oldColor;
 19InBlock.gif        oldColor = document.all('row'+rowno).style.backgroundColor;
 20InBlock.gif        document.all('row'+rowno).style.backgroundColor = newColor;
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif                
 23InBlock.gif        oldrow = document.all('row'+rowno);
 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif        
 26InBlock.gif        var checkFlag = true;
 27InBlock.gif    function ChooseAll()
 28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 29InBlock.gif       //if( !document.all("CheckAll").Checked ) // 全选 
 30InBlock.gif       if( checkFlag ) // 全选 
 31ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
 32InBlock.gif          var inputs = document.all.tags("INPUT");
 33InBlock.gif          for (var i=0; i < inputs.length; i++// 遍历页面上所有的 input 
 34ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
 35InBlock.gif            if (inputs[i].type == "checkbox" && inputs[i].id != "CheckAll" )
 36ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{
 37InBlock.gif                inputs[i].checked = true;
 38ExpandedSubBlockEnd.gif             }
     
 39ExpandedSubBlockEnd.gif          }

 40InBlock.gif          checkFlag = false;
 41ExpandedSubBlockEnd.gif       }

 42InBlock.gif       else  // 取消全选
 43ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 44InBlock.gif          var inputs = document.all.tags("INPUT");
 45InBlock.gif          for (var i=0; i < inputs.length; i++// 遍历页面上所有的 input 
 46ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
 47InBlock.gif             if (inputs[i].type == "checkbox" && inputs[i].id != "CheckAll" )
 48ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{
 49InBlock.gif                inputs[i].checked = false;
 50ExpandedSubBlockEnd.gif             }
     
 51ExpandedSubBlockEnd.gif          }

 52InBlock.gif          checkFlag = true;
 53ExpandedSubBlockEnd.gif       }

 54ExpandedSubBlockEnd.gif    }

 55InBlock.gif
 56InBlock.gif        // <summary>
 57InBlock.gif    // 让用户加以确认删除数据。
 58InBlock.gif    // </summary>
 59InBlock.gif    function DelRec()
 60ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 61InBlock.gif       var inputs = document.all.tags("input");
 62InBlock.gif       var selectedLen = 0;
 63InBlock.gif       forvar i=0;i < inputs.length; i ++)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 65InBlock.gif          if(inputs[i].type == "checkbox")
 66ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
 67InBlock.gif             if( inputs[i].checked )
 68ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{
 69InBlock.gif                if(inputs[i].id != "CheckAll")
 70ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 71InBlock.gif                   selectedLen ++;
 72ExpandedSubBlockEnd.gif                }

 73ExpandedSubBlockEnd.gif             }

 74ExpandedSubBlockEnd.gif          }

 75ExpandedSubBlockEnd.gif       }

 76InBlock.gif       if( selectedLen == 0 )
 77ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 78InBlock.gif          alert("请先选择您要删除的数据!");
 79ExpandedSubBlockEnd.gif         }

 80InBlock.gif       else
 81ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 82InBlock.gif          var flag = confirm("您确定要删除所选择的这 " + selectedLen + " 条数据吗?");
 83InBlock.gif          if(flag)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
 85InBlock.gif             document.all("ibtnDel").click();  
 86ExpandedSubBlockEnd.gif          }

 87ExpandedSubBlockEnd.gif       }

 88ExpandedSubBlockEnd.gif    }

 89InBlock.gif
 90ExpandedBlockEnd.gif
 91None.gif
</ script >
 92 None.gif < P >< FONT  face ="宋体" >
 93 None.gif         < TABLE  class ="TABLE"  id ="Table1"  style ="WIDTH: 771px; HEIGHT: 248px"  cellSpacing ="1"  cellPadding ="1"
 94 None.gif            width ="771"  align ="center"  border ="0" >
 95 None.gif             < TR >
 96 None.gif                 < TD >
 97 None.gif                     < P >
 98 None.gif                         < HR  color ="background"  SIZE ="1" >
 99 None.gif                        编号关键字:
100 None.gif                         < asp:textbox  id ="txtClassID"  runat ="server" ></ asp:textbox >< asp:imagebutton  id ="ibtnSearch"  runat ="server"  ImageUrl ="../../Images/button_search.GIF" ></ asp:imagebutton >
101 None.gif                    【按编号关键字进行搜索】
102 None.gif                 </ TD >
103 None.gif             </ TR >
104 None.gif             < TR >
105 None.gif                 < TD  style ="HEIGHT: 16px" >
106 None.gif                     < align ="left" > &nbsp;&nbsp;  品种名称:
107 None.gif                         < asp:textbox  id ="txtClassName"  runat ="server" ></ asp:textbox > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
108 None.gif                        价格:
109 None.gif                         < asp:textbox  id ="txtPrice"  runat ="server" ></ asp:textbox > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 备注:
110 None.gif                         < asp:textbox  id ="txtRemark"  runat ="server" ></ asp:textbox ></ P >
111 None.gif                 </ TD >
112 None.gif             </ TR >
113 None.gif             < TR >
114 None.gif                 < TD  style ="HEIGHT: 25px" >
115 None.gif                     < align ="right" >< asp:imagebutton  id ="ibtnAdd"  runat ="server"  ImageUrl ="../../Images/button_add.gif" ></ asp:imagebutton >< asp:imagebutton  id ="ibtnDel"  runat ="server"  ImageUrl ="../../Images/button_del.gif" ></ asp:imagebutton >< asp:imagebutton  id ="ibtnCancel"  runat ="server"  ImageUrl ="../../Images/button_cancel.gif" ></ asp:imagebutton ></ P >
116 None.gif                 </ TD >
117 None.gif             </ TR >
118 None.gif             < TR >
119 None.gif                 < TD  vAlign ="top" >
120 None.gif                     < DIV  align ="center" >< asp:datagrid  id ="DataGrid1"  runat ="server"  AllowSorting ="True"  AllowPaging ="True"  HorizontalAlign ="Center"
121 None.gif                            PageSize ="15"  Width ="100%"  AutoGenerateColumns ="False"  BorderColor ="#CCCCCC"  BorderStyle ="None"  BorderWidth ="1px"
122 None.gif                            BackColor ="White"  CellPadding ="3" >
123 None.gif                             < SelectedItemStyle  Font-Bold ="True"  ForeColor ="White"  BackColor ="#669999" ></ SelectedItemStyle >
124 None.gif                             < ItemStyle  ForeColor ="Black" ></ ItemStyle >
125 None.gif                             < HeaderStyle  Font-Bold ="True"  HorizontalAlign ="Center"  ForeColor ="White"  BackColor ="#6766CC" ></ HeaderStyle >
126 None.gif                             < FooterStyle  ForeColor ="#000066"  BackColor ="#6766CC" ></ FooterStyle >
127 None.gif                             < Columns >
128 None.gif                                 < asp:TemplateColumn >
129 None.gif                                     < ItemStyle  HorizontalAlign ="Center" ></ ItemStyle >
130 None.gif                                     < HeaderTemplate >
131 None.gif                                         < INPUT  id ="CheckAll"  onclick ="ChooseAll()"  type ="checkbox"  name ="CheckAll" >
132 None.gif                                     </ HeaderTemplate >
133 None.gif                                     < ItemTemplate >
134 None.gif                                         < asp:CheckBox  id ="chkDel"  runat ="server" ></ asp:CheckBox >
135 None.gif                                     </ ItemTemplate >
136 None.gif                                 </ asp:TemplateColumn >
137 None.gif                                 < asp:HyperLinkColumn  Target ="_self"  DataNavigateUrlField ="ClassID"  DataNavigateUrlFormatString ="Default.aspx?Module=SugarcaneClassInfoDetail&amp;ClassID={0}&amp;Mode=edit"
138 None.gif                                    DataTextField ="ClassID"  SortExpression ="ClassID"  HeaderText ="编号" >
139 None.gif                                     < HeaderStyle  Width ="10%"  CssClass ="HEADERSTYLE" ></ HeaderStyle >
140 None.gif                                     < ItemStyle  CssClass ="ITEMSTYLEHYPERLINK" ></ ItemStyle >
141 None.gif                                 </ asp:HyperLinkColumn >
142 None.gif                                 < asp:BoundColumn  Visible ="False"  DataField ="ClassID"  SortExpression ="ClassID"  HeaderText ="编号" ></ asp:BoundColumn >
143 None.gif                                 < asp:BoundColumn  DataField ="ClassName"  SortExpression ="ClassName"  HeaderText ="品种名称" >
144 None.gif                                     < HeaderStyle  Width ="30%" ></ HeaderStyle >
145 None.gif                                 </ asp:BoundColumn >
146 None.gif                                 < asp:BoundColumn  DataField ="Price"  SortExpression ="Price"  HeaderText ="价格"  DataFormatString ="{0:F2}" >
147 None.gif                                     < HeaderStyle  Width ="10%" ></ HeaderStyle >
148 None.gif                                 </ asp:BoundColumn >
149 None.gif                                 < asp:BoundColumn  DataField ="Remark"  HeaderText ="备注" >
150 None.gif                                     < HeaderStyle  Width ="28%" ></ HeaderStyle >
151 None.gif                                 </ asp:BoundColumn >
152 None.gif                                 < asp:EditCommandColumn  ButtonType ="LinkButton"  UpdateText ="更新"  CancelText ="取消"  EditText ="编辑" >
153 None.gif                                     < HeaderStyle  Width ="12%" ></ HeaderStyle >
154 None.gif                                 </ asp:EditCommandColumn >
155 None.gif                                 < asp:ButtonColumn  Text ="&lt;div οnclick=&quot;javascript:return confirm('确定删除吗?')&quot;&gt;删除&lt;/div&gt;"
156 None.gif                                    CommandName ="Delete" >
157 None.gif                                     < HeaderStyle  Width ="10%" ></ HeaderStyle >
158 None.gif                                 </ asp:ButtonColumn >
159 None.gif                             </ Columns >
160 None.gif                             < PagerStyle  Visible ="False"  HorizontalAlign ="Left"  ForeColor ="#000066"  BackColor ="#EAEAEA"  Mode ="NumericPages" ></ PagerStyle >
161 None.gif                         </ asp:datagrid ></ DIV >
162 None.gif                 </ TD >
163 None.gif             </ TR >
164 None.gif             < TR >
165 None.gif                 < TD >< asp:label  id ="lblPageCount"  runat ="server" ></ asp:label >< asp:label  id ="lblCurrentIndex"  runat ="server"  Width ="104px" ></ asp:label >< asp:linkbutton  id ="btnFirst"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
166 None.gif                        CommandArgument ="0"  ForeColor ="navy" ></ asp:linkbutton >< asp:linkbutton  id ="btnPrev"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
167 None.gif                        CommandArgument ="prev"  ForeColor ="navy" ></ asp:linkbutton >< asp:linkbutton  id ="btnNext"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
168 None.gif                        CommandArgument ="next"  ForeColor ="navy" ></ asp:linkbutton >< asp:linkbutton  id ="btnLast"  onclick ="PagerButtonClick"  runat ="server"  Font-Name ="verdana"  Font-size ="8pt"
169 None.gif                        CommandArgument ="last"  ForeColor ="navy" ></ asp:linkbutton >< asp:label  id ="Label1"  runat ="server" > 跳转: </ asp:label >< asp:textbox  id ="go"  Width ="20px"  BorderColor ="#9999FF"  BorderWidth ="1px"  BackColor ="White"  AutoPostBack ="True"
170 None.gif                        OnTextChanged ="goClick"  Runat ="server" ></ asp:textbox ></ TD >
171 None.gif             </ TR >
172 None.gif         </ TABLE >
173 None.gif     </ FONT >
174 None.gif </ P >
175 None.gif

转载于:https://www.cnblogs.com/pingkeke/archive/2006/08/26/486809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值