using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System;
using System.Configuration;

namespace lib6f_cs
{
    public interface IBonnesAffaires
    {
        DataSet liste { get; }
    }

    #region SearchWebPart
    public class SearchWebPart : WebPart, IBonnesAffaires
    {
        #region Propriété Title
        public override string Title
        {
            get
            {
                if (base.Title == string.Empty)
                    return "Les bonnes affaires en bourse";
                else
                    return base.Title;
            }
            set
            {
                base.Title = value;
            }
        }
        #endregion

        public SearchWebPart()
        {
        }

        #region Propriété CoursMax
        private double _CoursMax;

        [WebBrowsable(true)]
        [Personalizable(PersonalizationScope.User)]
        public double CoursMax
        {
            get
            {
                return _CoursMax;
            }

            set
            {
                _CoursMax = value;
            }
        }
        #endregion

        #region CreateChildControls
        private Label lbl_titre;
        private TextBox txt_cours;
        private Button cmd_rechercher;

        protected override void CreateChildControls()
        {
            lbl_titre = new Label();
            lbl_titre.Text = "Cours max";

            txt_cours = new TextBox();
            txt_cours.Text = CoursMax.ToString();

            cmd_rechercher = new Button();
            cmd_rechercher.Text = "Rechercher";
            cmd_rechercher.Click += new EventHandler(cmd_rechercher_Click);

            Controls.Add(lbl_titre);
            Controls.Add(txt_cours);
            Controls.Add(cmd_rechercher);
        }
        #endregion

        #region cmd_rechercher_click
        private DataSet _liste;
        void cmd_rechercher_Click(object sender, EventArgs e)
        {
            _CoursMax = double.Parse(txt_cours.Text);

            _liste = new DataSet();

            // alimenter la liste
            SqlConnection cx = new SqlConnection(ConfigurationManager.ConnectionStrings["bancaConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select societe,cours,evolution from bourse where cours<" + _CoursMax, cx);
            cmd.Connection = cx;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(_liste);
        }
        #endregion

        #region IBonnesAffaires Members

        public DataSet liste
        {
            get { return _liste; }
        }

        #endregion

        #region Interface de communication
        [ConnectionProvider("Liste")]
        public IBonnesAffaires GetCommunicationPoint()
        {
            return (IBonnesAffaires)this;
        }
        #endregion
    }
    #endregion

    #region ResultWebPart
    public class ResultWebPart : WebPart
    {
        #region CreateChildControls
        private GridView gv;

        protected override void CreateChildControls()
        {
            gv = new GridView();
            Controls.Add(gv);
        }
        #endregion

        #region Propriété Title
        public override string Title
        {
            get
            {
                if (base.Title == string.Empty)
                    return "La liste";

                return base.Title;
            }
            set
            {
                base.Title = value;
            }
        }
        #endregion

        #region Interface de communication
        private IBonnesAffaires affaires;

        [ConnectionConsumer("Liste")]
        public void InitializeProvider(IBonnesAffaires affaires)
        {
            this.affaires = affaires;
        }
        #endregion

        #region PreRender
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            EnsureChildControls();
            if (affaires != null)
            {
                gv.DataSource = affaires.liste;
                gv.DataBind();
            }
        }
        #endregion
    }
    #endregion
}