网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.用.net开发不同操作系统下应用的.
.对asp.net1.1开发模板类的一些修.
.在ASP.NET Atlas中调用Web Servi.
.在asp.net中实现删除DataGrid的某.
.有关TextBox中输入字符控制的一种.
.ASP.NET HTTP运行时组成详解.
.ASP.NET中不定级动态菜单的实现.
.GridView 分页导航.
.将dataset以xml形式发给客户端下.
..net 2.0中新增的nullable类型.
.ASP.NET模拟其他用户进行关机.
.使用MD5加密注册用户密码的简单示.
.得到内网可用的SqlServer 及某数.
.使用FileUpload控件上传图片并自.
.常用的匹配正则表达式和实例 .
.ASP.NET2.0下为GridView添加鼠标.
.Asp.net关于对url编码的转化 .
.如何在删除并重新安装 IIS 之后修.
.字符自动截取问题.
.asp.net WebForm页面间传值方法.

asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)

发表日期:2006-7-21


在asp.net 2.0下,gridview是十分方便的了,加一个DATASOURCE系列的控件的话,就可以马上和gridview绑定,十分方便。但其实也可以
使用datatable或者dataview的,这个时候就不是用datasource系列控件了。下面讲下如何在asp.net 2.0下,实现gridview控件的翻页,各列排序,
编辑的功能。
    首先,我们读取的是northwind数据库中的employee表。放置一个gridview后,添加几个绑定的列,代码如下
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
            Width="100%" DataKeyNames="EmployeeID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" PageSize="3" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="employeeid" HeaderText="Employee ID" ReadOnly="True" />
                <asp:BoundField DataField="firstname" HeaderText="First Name" SortExpression="firstname" />
                <asp:BoundField DataField="lastname" HeaderText="Last Name" SortExpression="lastname" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

首先,我们要实现分页,把AllowPaging设置为true,并设置每页的分页条数,最后在codebehind中写入
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
  为了实现每列都可以自动点击排序,可以设置allowsorting=true,然后设置OnSorting="GridView1_Sorting",其中gridview_sorting
代码为
  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortexpression"] = e.SortExpression;

        if (ViewState["sortdirection"] == null)
        {
            ViewState["sortdirection"] = "asc";
        }
        else
        {
            if (ViewState["sortdirection"].ToString() == "asc")
            {
                ViewState["sortdirection"] = "desc";
            }
            else
            {
                ViewState["sortdirection"] = "asc";
            }
        }
        BindGrid();
    }
很明显,设置viewsate来保存每次排序时的顺序,上面的相信很容易理解。
     最后,实现编辑功能,因为在aspx页面中已经设置了OnRowEditing="GridView1_RowEditing",其中GridView1_RowEditing的代码为
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int empid;
        string fname, lname;
        empid = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
        fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

        SqlConnection cnn = new SqlConnection(@"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
        cnn.Open();
        SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid", cnn);
        cmd.Parameters.Add(new SqlParameter("@fname",fname));
        cmd.Parameters.Add(new SqlParameter("@lname", lname));
        cmd.Parameters.Add(new SqlParameter("@empid", empid));
        cmd.ExecuteNonQuery();
        cnn.Close();

        GridView1.EditIndex = -1;
        BindGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }

  可以看到,上面的代码和asp.net 1.1版本的其实原理是差不多的。最后,bindgrid()的过程很简单,为绑定咯
DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
        da.Fill(ds,"employees");
        DataView dv = ds.Tables[0].DefaultView;

        if (ViewState["sortexpression"] != null)
        {
            dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
        }

        GridView1.DataSource=dv;
        GridView1.DataBind();

这里gridview绑定的是dataview,并且用dv.sort设定了每次排序的顺序,也就是说,每次排序后其顺序都是保持不变的。
当然最后是page_load事件咯
   protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGrid();
        }
    }

http://jackyrong.cnblogs.com/archive/2006/07/20/455996.html

上一篇:ASP.NET2.0下利用javascript实现TreeView中的checkbox全选 人气:7566
下一篇:页面根据不同Url显示不同Title以及不同的Mete 人气:5355
浏览全部asp.net的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐