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 :
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 <%@ 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>
Merci d'avance.
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); } }
Partager