using System;
using System.Data;
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;

using System.Data;
using System.Data.SqlClient;

public partial class test_viewstate_1 : System.Web.UI.Page
{
	

	protected void Page_Load(object sender, EventArgs e)
    {		
		if (!IsPostBack)
		{
			ViewState["tri"] = "titulaire";
			afficher();
		}		
    }

	private void afficher()
	{
		string tri;		
		tri = ViewState["tri"] as string;
		string rq = "SELECT titulaire, nom_agence FROM agence INNER JOIN compte ON agence.id_agence = compte.id_agence GROUP BY titulaire, nom_agence ORDER BY " + tri;

		SqlConnection cx = new SqlConnection(ConfigurationManager.ConnectionStrings["banca"].ConnectionString) ;
		SqlCommand sql = new SqlCommand(rq, cx);

		cx.Open();
		GridView1.DataSource = sql.ExecuteReader(CommandBehavior.CloseConnection);
		GridView1.DataBind();
	}

	protected void cmd_tri_titulaire_Click(object sender, EventArgs e)
	{
		ViewState["tri"] = "titulaire";
		afficher();
	}

	protected void cmd_agence_Click(object sender, EventArgs e)
	{
		ViewState["tri"] = "nom_agence";
		afficher();
	}
}
