IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

Génération automatique des templates field d'un gridView[Problème avec l'édition]


Sujet :

ASP.NET

  1. #1
    Membre averti
    Inscrit en
    Janvier 2007
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 32
    Par défaut Génération automatique des templates field d'un gridView[Problème avec l'édition]
    Bonjour
    j'essaie d'afficher les colones de ma source de données d'une façon dynamique à partir de la classe suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
     
    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.Collections.Specialized;
    using System.Data.SqlClient;
     
    public class DynamicallyTemplatedGridViewHandler : ITemplate
    {
        #region data memebers
     
        ListItemType ItemType;
        string FieldName;
        string InfoType;
     
        #endregion
     
        #region constructor
     
        public DynamicallyTemplatedGridViewHandler(ListItemType item_type, string field_name, string info_type)
        {
            ItemType = item_type;
            FieldName = field_name;
            InfoType = info_type;
        }
     
        #endregion
     
        #region Methods
     
        public void InstantiateIn(System.Web.UI.Control Container)
        {
            switch (ItemType)
            {
                case ListItemType.Header:
                    Literal header_ltrl = new Literal();
                    header_ltrl.Text = "<b>" + FieldName + "</b>";
                    Container.Controls.Add(header_ltrl);
                    break;
     
                case ListItemType.Item:
                    switch (InfoType)
                    {
                        case "Command":
                            ImageButton edit_button = new ImageButton();
                            edit_button.ID = "edit_button";
                            edit_button.ImageUrl = "~/Commun/Images/edit_small.png";
                            edit_button.CommandName = "Edit";
                            edit_button.Click += new ImageClickEventHandler(edit_button_Click);
                            edit_button.ToolTip = "Edit";
                            Container.Controls.Add(edit_button);
     
                            ImageButton delete_button = new ImageButton();
                            delete_button.ID = "delete_button";
                            delete_button.ImageUrl = "~/Commun/Images/delete2.png";
                            delete_button.CommandName = "Delete";
                            delete_button.ToolTip = "Delete";
                            //delete_button.OnClientClick = "return confirm('Are you sure to delete the record?')";
                            Container.Controls.Add(delete_button);
     
                            /* Similarly add button for insert.
                             * It is important to know when 'insert' button is added 
                             * its CommandName is set to "Edit"  like that of 'edit' button 
                             * only because we want the GridView enter into Edit mode, 
                             * and this time we also want the text boxes for corresponding fields empty*/
                            ImageButton insert_button = new ImageButton();
                            insert_button.ID = "insert_button";
                            insert_button.ImageUrl = "~/Commun/Images/add2.png";
                            insert_button.CommandName = "Edit";
                            insert_button.ToolTip = "Insert";
                            insert_button.Click += new ImageClickEventHandler(insert_button_Click);
                            Container.Controls.Add(insert_button);
     
                            break;
     
                        default:
                            Label field_lbl = new Label();
                            field_lbl.Text = String.Empty; //we will bind it later through 'OnDataBinding' event
                            field_lbl.DataBinding += new EventHandler(OnDataBinding);
                            Container.Controls.Add(field_lbl);
                            break;
     
                    }
                    break;
                case ListItemType.EditItem:
                    if (InfoType == "Command")
                    {
                        ImageButton update_button = new ImageButton();
                        update_button.ID = "update_button";
                        update_button.CommandName = "Update";
                        update_button.ImageUrl = "~/images/update.gif";
                        if ((int)new Page().Session["InsertFlag"] == 1)
                            update_button.ToolTip = "Add";
                        else
                        update_button.ToolTip = "Update";
                        //update_button.OnClientClick = "return confirm('Are you sure to update the record?')";
                        Container.Controls.Add(update_button);
     
                        ImageButton cancel_button = new ImageButton();
                        cancel_button.ImageUrl = "~/images/cancel.gif";
                        cancel_button.ID = "cancel_button";
                        cancel_button.CommandName = "Cancel";
                        cancel_button.ToolTip = "Cancel";
                        Container.Controls.Add(cancel_button);
     
                    }
                    else// for other 'non-command' i.e. the key and non key fields, bind textboxes with corresponding field values
                    {
                        TextBox field_txtbox = new TextBox();
                        field_txtbox.Text = String.Empty;
                         //if Inert is intended no need to bind it with text..keep them empty
                        if ((int)new Page().Session["InsertFlag"] == (int)EnumInsertFlag.Edit)
                        field_txtbox.DataBinding += new EventHandler(OnDataBinding);
                        Container.Controls.Add(field_txtbox);
     
                    }
                    break;
                case ListItemType.Footer:
                    if(InfoType!="Command")
                    {
                        TextBox MonTextBox = new TextBox();
                        MonTextBox.Text = String.Empty;
                        Container.Controls.Add(MonTextBox);
                    }
                    break;
            }
        }
     
        #endregion
     
        #region Event Handlers
     
        //just sets the insert flag ON so that we ll be able to decide in OnRowUpdating event whether to insert or update
        protected void insert_button_Click(Object sender,ImageClickEventArgs e)
        {
            new Page().Session["InsertFlag"] = (int)EnumInsertFlag.Insert;
        }
        //just sets the insert flag OFF so that we ll be able to decide in OnRowUpdating event whether to insert or update 
        protected void edit_button_Click(Object sender, ImageClickEventArgs e)
        {
            new Page().Session["InsertFlag"] = (int)EnumInsertFlag.Edit;
        }
     
        private void OnDataBinding(object sender, EventArgs e)
        {
     
            object bound_value_obj = null;
            Control ctrl = (Control)sender;
            IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
            bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
     
            switch (ItemType)
            {
                case ListItemType.Item:
                    Label field_ltrl = (Label)sender;
                    field_ltrl.Text = bound_value_obj.ToString();
     
                    break;
                case ListItemType.EditItem:
                    TextBox field_txtbox = (TextBox)sender;
                    field_txtbox.Text = bound_value_obj.ToString();
     
                    break;
            }
        }
     
        #endregion
     
     
    }
    pour l'affichage ce code marche à merveille mais qand j'édite ,les données ne s'affiche plus comme si la grille n'est plus Bind(é) ,voici le code Behind de ma page
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
     
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChargerGrilleMoyensFinanciers();
                LoadListeMoyensFinancier();
            }
        }
        private void LoadListeMoyensFinancier()
        {
            UmsProjet ProjetCourant = MonProjetService.RechercherProjetParId(IdProjet);
            DataSet1 MonDataSet = MonMoyenFinancierService.RecupererGrilleMoyenFinancier(ProjetCourant);
            ListeMoyensFinanciers.DataSource = MonDataSet.maTable;
            ListeMoyensFinanciers.DataBind();
            IList<UmsRubriqueBudgetaire> MesRubriquesBudgetaires = MaRubriqueBudgetService.Tout();
            DropDownList FooterListe = (DropDownList)ListeMoyensFinanciers.FooterRow.FindControl("ListeRubriquesBudgetaires");
            FooterListe.DataSource = MesRubriquesBudgetaires;
            FooterListe.DataBind();
        }
     
        private void ChargerGrilleMoyensFinanciers()
        {
            UmsProjet ProjetCourant = MonProjetService.RechercherProjetParId(IdProjet);
            DataSet1 MonDataSet = MonMoyenFinancierService.RecupererGrilleMoyenFinancier(ProjetCourant);
            DataTable UneTable = MonDataSet.maTable;
            //ListeMoyensFinanciers.Columns.Clear();
     
            foreach (DataColumn Column in UneTable.Columns)
            {
                if (Column.ColumnName != "Id" & Column.ColumnName!="Designation")
                {
                    TemplateField MyColumnTemplate = new TemplateField();
                    MyColumnTemplate.HeaderText = Column.ColumnName;
                    MyColumnTemplate.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item, Column.ColumnName, UneTable.Columns[Column.ColumnName].DataType.Name);
                    MyColumnTemplate.EditItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, Column.ColumnName, UneTable.Columns[Column.ColumnName].DataType.Name);
                    MyColumnTemplate.FooterTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Footer, Column.ColumnName, UneTable.Columns[Column.ColumnName].DataType.Name);
                    ListeMoyensFinanciers.Columns.Add(MyColumnTemplate);
                }
            }
            TemplateField MyButton = new TemplateField();
            MyButton.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item, "CRUD", "Command");
            MyButton.EditItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, "INSERT", "Command");
            ListeMoyensFinanciers.Columns.Add(MyButton);
        }
     
        public void Editer_MoyenFinancier(object sender,GridViewEditEventArgs e)
        {
            int RowIndex = e.NewEditIndex;
            ListeMoyensFinanciers.EditIndex = RowIndex;
            ListeMoyensFinanciers.Rows[RowIndex].RowState = DataControlRowState.Edit;
            ListeMoyensFinanciers.DataBind();
            Session["SelecetdRowIndex"] = e.NewEditIndex;
     
        }
        public void Supprimer_MoyenFinancier(object sender, GridViewDeleteEventArgs e)
        {
            ChargerGrilleMoyensFinanciers();
        }
        public void Annuler_Editer_MoyenFinancier(object sender, GridViewCancelEditEventArgs e)
        {
            ListeMoyensFinanciers.EditIndex = -1;
            ListeMoyensFinanciers.DataBind();
            Session["SelecetdRowIndex"] = -1; 
        }
        public void MAJ_MoyenFinancier(object sender, GridViewUpdateEventArgs e)
        {
            ChargerGrilleMoyensFinanciers();
        }
    je suis vraiment bloqué et j'attends vraiment vos remarques
    Merci

  2. #2
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Salut,

    Qu'a de particulier ton controle pour ne pas utiliser le GridView natif?

    A+
    "Winter is coming" (ma nouvelle page d'accueil)

  3. #3
    Membre averti
    Inscrit en
    Janvier 2007
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 32
    Par défaut
    Salut
    je génère les templates fields puisque je ne connais pas exactement le nombre des colonnes dans mon DataSet,en plus du fait que j'utilise une architecture trois tiers donc je n'ai pas d'accès direct à mes données
    merci et A +

  4. #4
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Salut,

    Ce n'est pas toujours possible, mais penses-tu que tu pourrais passer sur le Framework 3.5, LINQ et DynamicData.

    A+
    "Winter is coming" (ma nouvelle page d'accueil)

Discussions similaires

  1. Génération automatique des coordonnées de textures.
    Par F-fisher dans le forum OpenGL
    Réponses: 5
    Dernier message: 27/12/2009, 11h46
  2. outil de génération automatique des fichiers de mapping
    Par snoox87 dans le forum Hibernate
    Réponses: 4
    Dernier message: 12/08/2009, 16h14
  3. Réponses: 1
    Dernier message: 16/07/2009, 12h01
  4. Réponses: 1
    Dernier message: 15/04/2008, 12h00
  5. [JUnit] Génération automatique des classes de tests
    Par sidi.elmoctar dans le forum Tests et Performance
    Réponses: 1
    Dernier message: 05/08/2006, 00h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo