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 :

CascadingDropDown & SqlDataSource


Sujet :

ASP.NET

  1. #1
    Membre éclairé
    Inscrit en
    Février 2006
    Messages
    373
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 373
    Par défaut CascadingDropDown & SqlDataSource
    Bonjour,

    Je veux lier deux DropDownLists, tel que la première liste est chargée par les pays, et l'autre liste va être chargée par les villes corespondantes au pays sélectionnée dans la première liste --> c'est Ajax ! J'ai trouvé ce tutoriel:

    http://www.asp.net/learn/ajax-videos/video-77.aspx

    mais ceci utilise les données qui sont situées dans un fichier XML, alors que mes données existent dans une base de données, et je sais pas comment modifier le code de ce tutoriel pour lier les DropDownLists à SqlDataSource !!

    Voilà le code de ce tutoriel :

    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
     
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
     
            <table>
                <tr>
                    <td>Make</td>
                    <td><asp:DropDownList ID="DropDownList1" runat="server" Width="170" /></td>
                </tr>
                <tr>
                    <td>Model</td>
                    <td><asp:DropDownList ID="DropDownList2" runat="server" Width="170" /></td>
                </tr>
                <tr>
                    <td>Color</td>
                    <td><asp:DropDownList ID="DropDownList3" runat="server" Width="170" AutoPostBack="true"
                        OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" /></td>
                </tr>
            </table>
            <br />
     
            <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
                Category="Make"  PromptText="Please select a make"  LoadingText="[Loading makes...]"
                ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" />
            <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
                Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
                ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
            <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
                Category="Color" PromptText="Please select a color" LoadingText="[Loading colors...]"
                ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents"
                ParentControlID="DropDownList2" />
     
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="inline">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="[No response provided yet]" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
     
        </form>
    </body>
    </html>
    et

    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
     
    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.Web.Services;
    using System.Collections.Specialized;
    using AjaxControlToolkit;
     
    public partial class _Default : System.Web.UI.Page
    {
     
       protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get selected values
            string make = DropDownList1.SelectedItem.Text;
            string model = DropDownList2.SelectedItem.Text;
            string color = DropDownList3.SelectedItem.Text;
     
            // Output result string based on which values are specified
            if (string.IsNullOrEmpty(make))
            {
                Label1.Text = "Please select a make.";
            }
            else if (string.IsNullOrEmpty(model))
            {
                Label1.Text = "Please select a model.";
            }
            else if (string.IsNullOrEmpty(color))
            {
                Label1.Text = "Please select a color.";
            }
            else
            {
                Label1.Text = string.Format("You have chosen a {0} {1} {2}. Nice car!", color, make, model);
            }
        }
     
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
        {
            return new CarsService().GetDropDownContents(knownCategoryValues, category);
        }
     
     
    }
    Merci d'avance.

  2. #2
    Membre éclairé
    Inscrit en
    Février 2006
    Messages
    373
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 373
    Par défaut
    J'ai essayé de trouver une autre solution sans utiliser CascadingDropDown, mais maintenant j'ai besoin encore de la soultion de ce pb. J'attends votre aide et merci beaucoup.

  3. #3
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Je comprends pas ton problème.
    Dans l'exemple, là où ils lisent un fichier XML, bah toi tu lis en base

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