基于gridview的三层结构的代码演示 (二 ) 三层架构的实例演示 (原创,如需转载请联系作者)

来源:互联网 发布:抓取数据的十大算法 编辑:程序博客网 时间:2024/05/21 05:58

引言:

前文已经介绍了用gridview做为显示的三层架构实例,增删改查作为针对数据库的基本和所有操作,很显然有必要给大家说下,gridview这个控件是怎样完成这些操作的。

 

正文:

类文件用上一篇我所提到的三个类文件,先给出代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            
int num = new SLogic().GetNum("select count(sid) from student");
            
//数据库中的表中没有记录
            if (num == 0)
            
{
                Session[
"pgnum"= 0;
                Session[
"pg"= 0;
            }

            
else
            

                
int pgnum = (num % 3 == 0? num / 3 : num / 3 + 1;
                Session[
"pgnum"= pgnum;
                Session[
"pg"= 1;
            }

            GridView1.DataSource 
= new SLogic().GetList("select top 3 * from student");
            GridView1.DataBind();
        }

    }


    
//=======================自定义分页==========================
    protected void LinkButton1_Click(object sender, EventArgs e)
    
{
        
int pgnum = int.Parse(Session["pgnum"].ToString());
        
int pg = int.Parse(Session["pg"].ToString());
        
//有数据才能分页
        if (pgnum > 0)
        
{
            LinkButton lb 
= (LinkButton)sender;
            
switch (lb.CommandName.ToString())
            
{
                
case "a1":
                    pg 
= 1;
                    
break;
                
case "a2":
                    pg
--;
                    
break;
                
case "a3":
                    pg
++;
                    
break;
                
case "a4":
                    pg 
= pgnum;
                    
break;
                
default:
                    pg 
= pgnum;
                    
break;
            }

            
if (pg < 1)
            
{
                pg 
= 1;
            }

            
if (pg > pgnum)
            
{
                pg 
= pgnum;
            }

            Session[
"pg"= pg;
            
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
            GridView1.DataSource 
= new SLogic().GetList(strsql);
            GridView1.DataBind();
        }

        
else
        
{
            Response.Write(
"<script>alert('对不起,没有记录,不能分页!');location.reload('default.aspx');</script>");
        }

    }

    
    
//=======================增加=========================
    protected void btnadd_Click(object sender, EventArgs e)
    
{
        
string sname = tbname.Text;
        
bool badd = new SLogic().Add(sname);
        tbname.Text 
= "";

        
//增加完一条数据后,显示最后一页
        int num = new SLogic().GetNum("select count(sid) from student");
        
int pgnum = (num % 3 == 0? num / 3 : num / 3 + 1;        
        
//HiddenField1.Value = strsql;
        Session["pgnum"= pgnum;
        Session[
"pg"= pgnum;
        
string strsql = "select top 3 * from student where sid not in (select top " + (pgnum - 1* 3 + " sid from student)";
        GridView1.DataSource 
= new SLogic().GetList(strsql);
        GridView1.DataBind();
    }


    
//========================删除========================
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
int sid = int.Parse(this.GridView1.Rows[e.RowIndex].Cells[0].Text.ToString());
        
bool bdel = new SLogic().Delete(sid);
        
//删除完之后要做判断
        int pgnum = int.Parse(Session["pgnum"].ToString());
        
int pg = int.Parse(Session["pg"].ToString());        
        
if (pgnum > 1)//有大于一页的数据
        {
            
if (GridView1.Rows.Count == 1)//删除的是最后一页的最后一条
            {
                pg
--;
                
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
                Session[
"pg"= pg;
                Session[
"pgnum"= pg;
                GridView1.DataSource 
= new SLogic().GetList(strsql);
                GridView1.DataBind();
            }

            
else
            
{
                
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
                GridView1.DataSource 
= new SLogic().GetList(strsql);
                GridView1.DataBind();  
            }

        }

        
//只有一页
        else
        
{
            
//只有一条记录
            if (GridView1.Rows.Count == 1)//删除的是最后一页的最后一条
            {
                
string strsql = "select top 3 * from student";
                Session[
"pg"= 0;
                Session[
"pgnum"= 0;
                GridView1.DataSource 
= new SLogic().GetList(strsql);
                GridView1.DataBind();
            }

            
else
            
{
                
string strsql = "select top 3 * from student";
                GridView1.DataSource 
= new SLogic().GetList(strsql);
                GridView1.DataBind();
            }

        }

    }

    
//====================在删除时提示是否删除===================
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Cells[
4].Attributes.Add("onclick""javascript:return confirm('确定要删除吗?');");
            
//e.Row.Cells[4].Attributes.Add("onclick", "javascript:if(!confirm('确定要删除吗?')) return false;");           
            
//注释掉的上面这句话说关于页面不提交的什么说法,我测试的好像没有什么区别
        }

    }


    
//把当前行变成编辑状态
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        
int pg = int.Parse(Session["pg"].ToString());
        
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
        GridView1.DataSource 
= new SLogic().GetList(strsql);
        GridView1.DataBind();
    }

    
//取消编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        
int pg = int.Parse(Session["pg"].ToString());
        
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
        GridView1.DataSource 
= new SLogic().GetList(strsql);
        GridView1.DataBind();
    }


    
//====================修改===================
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
int id = int.Parse(this.GridView1.Rows[e.RowIndex].Cells[0].Text.ToString());
        TextBox tb 
= this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0as TextBox;
        
string name = tb.Text;
        
bool bupdate = new SLogic().Update(id, name);

        
//把当前行的修改状态清除掉
        GridView1.EditIndex = -1;

        
//修改完之后定位到当前页
        int pg = int.Parse(Session["pg"].ToString());
        
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
        GridView1.DataSource 
= new SLogic().GetList(strsql);
        GridView1.DataBind();
    }


    
//====================选择===================
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{  
        
int pg = int.Parse(Session["pg"].ToString());
        
string strsql = "select top 3 * from student where sid not in (select top " + (pg - 1* 3 + " sid from student)";
        GridView1.DataSource 
= new SLogic().GetList(strsql);
        GridView1.DataBind();
        
for (int i = 0; i < this.GridView1.Rows.Count; i++)
        
{
            
if (this.GridView1.Rows[i].RowIndex == this.GridView1.SelectedRow.RowIndex)
            
{
                
this.GridView1.SelectedRow.BackColor = System.Drawing.Color.Pink;
            }

        }

    }

}

Page_Load方法里,我们用了两个session对象分别存储页面的总页数和当前页面的页面数,页面的总页数是按照数据的总条数与一次显示多少条数据来计算的。然后再把第一页的数据绑定到gridview

LinkButton1_Click(object sender, EventArgs e)方法里面,我们来进行分页,这里面我们需要注意的是分页的sql语句,select top 3 * from student where sid not in (select top " + (pg - 1) * 3 + " sid from student), 之前我们要将sender拆箱为linkbutton类型,来判断用户点击的是4个和页面有关的linkbutton的哪一个。里面的逻辑判断很容易,读者自行看看便知。需要注意的是如果删除一条数据的时候造成了页面的页数少了一页,这种情况会发生在如果最后一个页面只有一条数据,这时候无论你删除了哪个页面的任何一条数据都会造成页面数减一。

增加过程中,因为数据表student的设计的sid字段是自增型的,所以增加姓名的同时,sid字段会自增,,只需在增加完成后显示最后一页就可以了。

删除时一定会有个提示框,选中我所做的操作时改变该行的颜色。

 

原创粉丝点击