我现在使用的翻页和排序都是GridView自带的...

解决方案 »

  1.   

    用viewstate保存你的排序字段,每次翻页重新绑定时,查询都order by该viewstate
      

  2.   

    请问用javascript该怎么实现?哪位大哥有没有实例...
      

  3.   

    ps:不是js
    private void BindDG()
    {
    SqlConnection con=new SqlConnection("server=.;database=rmt2;uid=sa;pwd=000");
    SqlDataAdapter sda=new SqlDataAdapter("select * from cityview",con);
    DataSet ds=new DataSet();
    sda.Fill(ds,"city");
    DataView dv=ds.Tables["city"].DefaultView;
    dv.Sort=this.DataGrid1.Attributes["SortExp"]+" "+this.DataGrid1.Attributes["SortDir"];
    this.DataGrid1.DataSource=dv;
    this.DataGrid1.DataBind(); con.Close();
    }
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!this.IsPostBack)
    {
    this.DataGrid1.Attributes["SortExp"]="provincename";
    this.DataGrid1.Attributes["SortDir"]="asc";
    this.BindDG();
    }
    // 在此处放置用户代码以初始化页面
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    this.BindDG();
    } private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    if(e.SortExpression!=null && e.SortExpression==this.DataGrid1.Attributes["SortExp"])
    {
    this.DataGrid1.Attributes["SortDir"]=(this.DataGrid1.Attributes["SortDir"]=="asc"?"desc":"asc");
    }
    else
    {
    this.DataGrid1.Attributes["SortExp"]=e.SortExpression;
    this.DataGrid1.Attributes["SortDir"]="asc";
    }
    this.BindDG();
    }
    }