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 :

[C#] problème rendu custom web control


Sujet :

ASP.NET

  1. #1
    Membre régulier
    Inscrit en
    Avril 2005
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 8
    Par défaut [C#] problème rendu custom web control
    Bonjour,

    j'ai créé un custom web control comportant un zone de saisie éditable.
    On peut y ajouter du texte ou d'autres web controls via le designer par un simple glissé déposé.

    Tout marche très bien dans le designer, je peux par exemple ajouter 2 boutons.

    Le problème c'est que lorsque je lance ma page asp.net, j'ai bien le cadre (la couleur de fond) de mon custom web control mais aucun bouton n'est ajouté.

    Savais-vous d'où peut venir le problème ?
    Merci


    Voici le code :

    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
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.Web.UI.WebControls;
     
    namespace Samples.ASPNet.ControlDesigners_CS
    {
        [Designer(typeof(MyMultiRegionControlDesigner)),
        ToolboxData("<{0}:MyMultiRegionControl runat=\"server\" width=\"200\"></{0}:MyMultiRegionControl>")]
        public class MyMultiRegionControl : CompositeControl
        {
            // Define the templates that represent 2 views on the control
            private ITemplate _view;
     
            // Create persistable inner properties 
            // for the two editable views
            [PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(null)]
            public virtual ITemplate View
            {
                get { return _view; }
                set { _view = value; }
            }
     
            // Create a simple table with a row of two clickable, 
            // readonly headers and a row with a single column, which 
            // is the 'container' to which we'll be adding controls.
            protected override void CreateChildControls()
            {
                // Always start with a clean form
                Controls.Clear();
     
                // Create a table using the control's declarative properties
                Table t = new Table();
                t.CellSpacing = 1;
                t.BorderStyle = BorderStyle;
                t.Width = this.Width;
                t.Height = this.Height;
     
                // Create the header row
                TableRow tr = new TableRow();
                tr.HorizontalAlign = HorizontalAlign.Center;
                tr.BackColor = Color.LightBlue;
                tr.HorizontalAlign = HorizontalAlign.Center;
     
                // This cell represents our content 'container'
                TableCell tc = new TableCell();
                tc.Height = new Unit(20);
                tr.Cells.Add(tc);
     
                t.Rows.Add(tr);
     
                // Add the finished table to the Controls collection
                Controls.Add(t);
            }
     
        }
     
        //---------------------------------------------------------
        // Region-based control designer for the above web control, 
        // derived from CompositeControlDesigner.
        public class MyMultiRegionControlDesigner : CompositeControlDesigner
        {
            private MyMultiRegionControl myControl;
     
            public override void Initialize(IComponent component)
            {
                base.Initialize(component);
                myControl = (MyMultiRegionControl)component;
            }
     
            // Make this control resizeable on the design surface
            public override bool AllowResize
            {
                get
                {
                    return true;
                }
            }
     
            // Use the base to create child controls, then add region markers
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                // Get a reference to the table, which is the first child control
                Table t = (Table)myControl.Controls[0];
     
                // Add design time markers for each of the three regions
                if (t != null)
                {
                    t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "0";
                }
            }
     
     
            // Create the regions and design-time markup. Called by the designer host.
            public override String GetDesignTimeHtml(DesignerRegionCollection regions)
            {
                // Create an editable region and add it to the regions
                EditableDesignerRegion editableRegion =
                    new EditableDesignerRegion(this,
                        "Content", false);
                regions.Add(editableRegion);
     
                // Use the base class to render the markup
                return base.GetDesignTimeHtml();
            }
     
            // Get the content string for the selected region. Called by the designer host?
            public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
            {
                // Get a reference to the designer host
                IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                if (host != null)
                {
                    ITemplate template = myControl.View;
     
                    // Persist the template in the design host
                    if (template != null)
                        return ControlPersister.PersistTemplate(template, host);
                }
     
                return String.Empty;
            }
     
            // Create a template from the content string and  
            // put it in the selected view.
            public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
            {
                if (content == null)
                    return;
     
                // Get a reference to the designer host
                IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                if (host != null)
                {
                    // Create a template from the content string
                    ITemplate template = ControlParser.ParseTemplate(host, content);
     
                    if (template != null)
                    {
                        myControl.View = template;
                    }
                }
            }
        }
    }

  2. #2
    Membre régulier
    Inscrit en
    Avril 2005
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 8
    Par défaut
    Bon ben à force de chercher et rechercher, j'ai trouvé la solution à mon problème en me basant sur ces deux articles de la msdn.

    Je ne sais pas si c'est la meilleur solution, mais elle a le mérite de fonctionner.

    Ne me demandez pas par contre des détails sur le fonctionnement exact de "ma" solution, car moi-même je me demande parfois comment j'ai pu le faire

    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
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.Web.UI.WebControls;
     
    namespace Samples.ASPNet.ControlDesigners_CS
    {
        [Designer(typeof(MyMultiRegionControlDesigner)),
        ToolboxData("<{0}:MyMultiRegionControl runat=\"server\" width=\"200\"></{0}:MyMultiRegionControl>")]
        public class MyMultiRegionControl : CompositeControl
        {
            private Control myTemplateContainer;
     
            // Define the templates that represent 2 views on the control
            private ITemplate _view;
     
            // Create persistable inner properties 
            // for the two editable views
            [DefaultValue(null),
            PersistenceMode(PersistenceMode.InnerProperty)
            ,TemplateContainer(typeof(ViewContainer))]   
            public virtual ITemplate MyView
            {
                get { return _view; }
                set { _view = value; }
            }
     
            // Create a simple table with a row of two clickable, 
            // readonly headers and a row with a single column, which 
            // is the 'container' to which we'll be adding controls.
            protected override void CreateChildControls()
            {
                // Always start with a clean form
                Controls.Clear();
     
                // Create a table using the control's declarative properties
                Table t = new Table();
                t.CellSpacing = 1;
                t.BorderStyle = BorderStyle;
                t.Width = this.Width;
                t.Height = this.Height;
     
                // Create the header row
                TableRow tr = new TableRow();
                tr.HorizontalAlign = HorizontalAlign.Center;
                tr.BackColor = Color.LightBlue;
     
                // This cell represents our content 'container'
                TableCell tc = new TableCell();
                tc.Height = new Unit(20);
     
                //----------------------------------------------------
                //----------------------------------------------------
                if (MyView != null)
                {
                    myTemplateContainer = new ViewContainer(this);
                    MyView.InstantiateIn(myTemplateContainer);
                    tc.Controls.Add(myTemplateContainer);
                }
                //----------------------------------------------------
                //----------------------------------------------------
     
                tr.Cells.Add(tc);
                t.Rows.Add(tr);
     
                // Add the finished table to the Controls collection
                Controls.Add(t);
     
            }
     
     
            protected override void OnDataBinding(EventArgs e)
            {
                EnsureChildControls();
                base.OnDataBinding(e);
            }
        }
     
     
     
     
     
     
     
        public class ViewContainer : Control, INamingContainer
        {
            private MyMultiRegionControl parent;
            public ViewContainer(MyMultiRegionControl parent)
            {
                this.parent = parent;
            }
        }
     
     
     
     
     
     
     
        //---------------------------------------------------------
        // Region-based control designer for the above web control, 
        // derived from CompositeControlDesigner.
        public class MyMultiRegionControlDesigner : CompositeControlDesigner
        {
            private MyMultiRegionControl myControl;
     
            public override void Initialize(IComponent component)
            {
                base.Initialize(component);
                myControl = (MyMultiRegionControl)component;
            }
     
            // Make this control resizeable on the design surface
            public override bool AllowResize
            {
                get
                {
                    return true;
                }
            }
     
            // Use the base to create child controls, then add region markers
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                // Get a reference to the table, which is the first child control
                Table t = (Table)myControl.Controls[0];
     
                // Add design time markers for each of the three regions
                if (t != null)
                {
                    t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "0";
                }
            }
     
     
            // Create the regions and design-time markup. Called by the designer host.
            public override String GetDesignTimeHtml(DesignerRegionCollection regions)
            {
                // Create an editable region and add it to the regions
                EditableDesignerRegion editableRegion =
                    new EditableDesignerRegion(this,
                        "Content", false);
                regions.Add(editableRegion);
     
                // Use the base class to render the markup
                return base.GetDesignTimeHtml();
            }
     
            // Get the content string for the selected region. Called by the designer host?
            public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
            {
                // Get a reference to the designer host
                IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                if (host != null)
                {
                    ITemplate template = myControl.MyView;
     
                    // Persist the template in the design host
                    if (template != null)
                        return ControlPersister.PersistTemplate(template, host);
                }
     
                return String.Empty;
            }
     
            // Create a template from the content string and  
            // put it in the selected view.
            public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
            {
                if (content == null)
                    return;
     
                // Get a reference to the designer host
                IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                if (host != null)
                {
                    // Create a template from the content string
                    ITemplate template = ControlParser.ParseTemplate(host, content);
     
                    if (template != null)
                    {
                        myControl.MyView = template;
                    }
                }
            }
        }
    }
    Si vous avez des suggestions ou des améliorations à proposer, n'hésitez pas !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ASP 1.1/C#] Custom Web Control et ViewState
    Par User.Anonymous dans le forum ASP.NET
    Réponses: 1
    Dernier message: 07/03/2008, 14h52
  2. le web controle:dropdownlist
    Par tsdia2 dans le forum ASP.NET
    Réponses: 5
    Dernier message: 03/05/2007, 08h31
  3. web controle ?
    Par WELCOMSMAIL dans le forum ASP.NET
    Réponses: 1
    Dernier message: 01/02/2007, 09h31
  4. [C#] Web Control - Changement valeur par défaut
    Par Dozer71 dans le forum ASP.NET
    Réponses: 2
    Dernier message: 24/08/2006, 15h28
  5. [C#]web control user dynamique??
    Par norkius dans le forum ASP.NET
    Réponses: 3
    Dernier message: 11/12/2003, 18h35

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