zoukankan      html  css  js  c++  java
  • 在GridView中进行排序

          点击列头,可以将列字段按照升降序进行排序。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <Columns>
                        <asp:BoundField DataField="UserID" HeaderText="用户ID" ReadOnly="True" SortExpression="UserID"/>
                        <asp:BoundField DataField="User_Nm" HeaderText="用户姓名" SortExpression="User_Nm" />
                        <asp:BoundField DataField="User_Sex" HeaderText="性别" SortExpression="User_Sex"/>
                        <asp:BoundField DataField="User_Address" HeaderText="家庭住址" SortExpression="User_Address" />
                        <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                        <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                        <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                    </Columns>
                    <RowStyle ForeColor="#008066" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                </asp:GridView>

          

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    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 Default2 : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        string strCon = "Data Source=.\\SQL2005;DataBase=Users;uid=sa;pwd=123";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "User_Nm";
                ViewState["OrderDire"] = "ASC";
                bind();
            }
        }

        public void bind()
        {
            string sqlstr = "SELECT TOP 10 * from Users";
            sqlcon = new SqlConnection (strCon );
            SqlDataAdapter myda = new SqlDataAdapter (sqlstr ,sqlcon );

            DataSet myds = new DataSet ();
            sqlcon .Open ();

            myda.Fill(myds, "Us");
            DataView view = myds.Tables["Us"].DefaultView;
            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            view.Sort = sort;
            GridView1.DataSource = view;
            GridView1.DataBind();
            sqlcon.Close();

        }
    protected void  GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression ;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "DESC")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "DESC";
            }
        }
      
        bind();
    }
    }

  • 相关阅读:
    Wireshark按照域名过滤
    【黑客免杀攻防】读书笔记12
    【逆向知识】反调试-除0异常-编程与逆向
    JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
    【Python】HackBack(获取暴力破解服务器密码的IP来源)
    【Python】CVE-2017-10271批量自查POC(Weblogic RCE)
    【Python】exe2shellcode,shellcode2exe
    【Python】如何基于Python写一个TCP反向连接后门
    【工具】用命令行与Python使用YARA规则
    选择排序
  • 原文地址:https://www.cnblogs.com/daiweixm/p/1525451.html
Copyright © 2011-2022 走看看