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 :

Ajouter dynamiquement un bouton radio à un gridview


Sujet :

ASP.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2007
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2007
    Messages : 236
    Par défaut Ajouter dynamiquement un bouton radio à un gridview
    Bonjour, j'ai un grand problème, ça fait une journée que cherche sur le net mais malheureusement sans résultat. Je souhaite ajouter à mon gridview un bouton radio à chaque ligne.
    - Mon grid est contenu dans une webpart dans le code utilisé est uniquement c#.
    - Je dois gérer le post back quand je sélectionne un radio.
    - Je dois sélectionner une seule ligne comme en HTML en spécifiant le nom, mais avec asp.net c'est pas faisable.

    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
     
    using System;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
     
    using System.Xml.Serialization;
    using System.Data;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
     
    namespace ProjetSPGridView
    {
        class RadioButtonTemplateField : ITemplate
        {
        private DataControlRowType templateType;
        private string columnName;
        private SPGridView grid;
     
        public RadioButtonTemplateField(DataControlRowType type, string colname,SPGridView grid)
        {
            templateType = type;
            columnName = colname;
            this.grid = grid;
        }
     
        void ITemplate.InstantiateIn(Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    RadioButton rb = new RadioButton();
                    rb.AutoPostBack = true;
                    rb.ID = "rb1";
                    rb.EnableViewState = true;
                    //DataTable dt = (DataTable)grid.DataSource;
                    //rb.Text = DataBinder.Eval(container.i;
     
                    rb.DataBinding += delegate(object sender, EventArgs e)
                    {
                        GridViewRow row = (GridViewRow)rb.NamingContainer;
     
                        int ID = (int)DataBinder.Eval(row.DataItem, "ID");
     
                        ((RadioButton)sender).Text = ID.ToString();
     
                    };
     
     
                    rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
     
                    //LiteralControl c = new LiteralControl("radio");
                    //c.ID = "radio";
                    //c.Controls.Add(rb);
     
                    //rb.Text = "radio";
                    //rb.Items.Add("aa");
                    //rb.SelectedIndex = 0;
                    //rb.DataValueField=DataBinder.Eval(container.data
                    //container.Controls.Add(rb);
                    container.Controls.Add(rb);
                    break;
                default:
                    break;
            }
        }
     
     
     
        void rb_CheckedChanged(object sender, EventArgs e)
        {
            ((Page)sender).Response.Write("<script>bonjour</script>");
            RadioButton rb = new RadioButton();
            rb = (RadioButton)sender;
            string str = rb.Text;
            SPGridView grid = (SPGridView)sender;
            RadioButton rbgrid;
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                rbgrid = (RadioButton)grid.Rows[i].FindControl("rb1");
                rbgrid.Checked = false;
                if (str == rbgrid.Text)
                {
                    rbgrid.Checked = true;
                    ((Page)sender).Session["nomCheck"] = rbgrid.Text;
     
                }
     
            }
     
        }
     
     
        }
    }
    Le code de la webpart :

    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
     
    protected override void CreateChildControls()
            {
     
                    base.CreateChildControls();
                    GetData();
     
                    oView.Table = datatable;
                    oGrid.ID = "UserGrid";
                    oGrid.DataSource = oView;
                    oGrid.AutoGenerateColumns = false;
                    oGrid.RowDataBound +=new GridViewRowEventHandler(oGrid_RowDataBound);
                    //oGrid.RowDataBound += new EventHandler(oGrid_DataBound);
                    oGrid.AllowSorting = true;
                    oGrid.RowCreated += new GridViewRowEventHandler(oGrid_RowCreated);
                    oGrid.SelectedIndexChanged += new EventHandler(oGrid_SelectedIndexChanged);
                    oGrid.SelectedIndexChanging += new GridViewSelectEventHandler(oGrid_SelectedIndexChanging);
                    oGrid.CssClass = "ms-listviewtable";
                    oGrid.AlternatingRowStyle.CssClass = "ms-alternating";
                    oGrid.Width = new Unit(50, UnitType.Percentage);
                    oGrid.GridLines = GridLines.None;
                    oGrid.HeaderStyle.Font.Bold = true;
                    oGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
     
     
                    BoundField colTitle;
                    for (int i = 0; i < datatable.Columns.Count; i++)
                    {
                        colTitle = new BoundField();
                        colTitle.DataField = datatable.Columns[i].ColumnName;
                        colTitle.HeaderText = datatable.Columns[i].ColumnName;
                        colTitle.SortExpression = datatable.Columns[i].ColumnName;
                        this.oGrid.Columns.Add(colTitle);
                    }
                    TemplateField customField = new TemplateField();
                    customField.ShowHeader = true;
                    customField.HeaderTemplate = new RadioButtonTemplateField(DataControlRowType.Header, "", oGrid);
                    customField.ItemTemplate = new RadioButtonTemplateField(DataControlRowType.DataRow, " ", oGrid);
     
                    oGrid.Columns.Add(customField);
     
     
                    this.Controls.Add(oGrid);
     
     
            }
     
            void oGrid_RowCreated(object sender, GridViewRowEventArgs e)
            {
                //SPGridViewRow row = (SPGridViewRow)sender;
                //if(oGrid.Rows[0]==row)
                //{
                //    RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
                //    rbtnl.Checked = true;  
                // }
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
     
                    RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
                    if (rbtnl.Text.Equals(Page.Session["nomCheck"]))
                        rbtnl.Checked = true;
     
                }
            }
     
     
     
            void oGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
            {
                Page.Response.Write("<script>e.NewSelectedIndex</script>") ;
            }
     
            void oGrid_SelectedIndexChanged(object sender, EventArgs e)
            {
     
            }
     
    protected void oGrid_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
     
                    RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
                    if (rbtnl.Text.Equals(Page.Session["nomCheck"]))
                        rbtnl.Checked = true;
     
                }
            }

  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,

    Tu ne peux pas utiliser le bouton "Select" du gridview? Cela fait parti des boutons de commande comme "Delete", etc.

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

  3. #3
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2007
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2007
    Messages : 236
    Par défaut
    Bonjour, j'ai besoin d'un bouton radio !

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    9
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2009
    Messages : 9
    Par défaut
    Bonjour,

    J'avais déjà cherché pour ajouter un Radio Button à la place du bouton Select !

    Et j'avais trouvé un bon tuto qui marche sans problème, mais impossible de le retrouver là.

    Cependant regarde ce lien, ça peut peut-être t'aider !

    http://www.progtalk.com/ViewArticle.aspx?ArticleID=25

Discussions similaires

  1. Réponses: 13
    Dernier message: 31/07/2013, 23h32
  2. Ajout d'un bouton radio en dynamique
    Par rouls dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 25/08/2011, 18h30
  3. Ajout dynamique de bouton de défilement
    Par ludogoal dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 27/05/2008, 13h45
  4. Ajouter dynamiquement un bouton
    Par toure32 dans le forum Delphi
    Réponses: 2
    Dernier message: 16/04/2007, 23h40
  5. Formulaire dynamique et bouton radio
    Par Analfabete dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 05/01/2007, 20h56

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