Bonjour,
Aprés avoir modifié le EditItemTemplate d'un DetailsView en remplaçant un label par un contrôle de type DropDown, j'obtiens l'exceptionCette erreur survient au moment du DeatailsView.Databind (lorsque je veux recharger le contenu de ma DropDown aprés insertion, modification ou suppression d'un des éléments contenus dans ma table Sql).InvalidOperationException : Les méthodes de liaison de données telles que Eval(), XPath() et Bind() peuvent uniquement être utilisées dans le contexte d'un contrôle lié aux données.
Je fais pourtant la même opération dans une page précédente (ajout, modification et suppression de catégories et affichage de toutes les catégories dans une DropDownlist) et ça fonctionne bien. La seule différence est que je ne l'utilise pas dans un DetailsView sur cette page.
Voici le code ASP :
Et voici le code behind d'un des trois boutons qui gère l'ajout de catégories :
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 <asp:SqlDataSource ID="dsProduit" runat="server" ConnectionString="<%$ ConnectionStrings:csSigneEtSens %>" DeleteCommand="DELETE FROM [Produit] WHERE [Prod_Id] = @Prod_Id" SelectCommand="SELECT * FROM [Produit] INNER JOIN [Categorie] ON Produit.Cat_Id = Categorie.Cat_Id WHERE ([Prod_Id] = @Prod_Id)" UpdateCommand="UPDATE [Produit] SET [Prod_Nom] = @Prod_Nom, [Prod_Texte] = @Prod_Texte, [Prod_Prix] = @Prod_Prix, [Prod_Image] = @Prod_Image, [Cat_Id] = @Cat_Id WHERE [Prod_Id] = @Prod_Id"> <SelectParameters> <asp:QueryStringParameter Name="Prod_Id" QueryStringField="ID" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="Prod_Id" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Prod_Nom" Type="String" /> <asp:Parameter Name="Prod_Texte" Type="String" /> <asp:Parameter Name="Prod_Prix" Type="Decimal" /> <asp:Parameter Name="Prod_Image" Type="String" /> <asp:Parameter Name="Cat_Id" Type="Int32" /> <asp:Parameter Name="Prod_Id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="Prod_Id" DataSourceID="dsProduit"> <Fields> <asp:BoundField DataField="Prod_Id" HeaderText="Référence" InsertVisible="False" ReadOnly="True" SortExpression="Prod_Id" /> <asp:BoundField DataField="Prod_Nom" HeaderText="Nom de Produit" SortExpression="Prod_Nom" /> <asp:BoundField DataField="Prod_Texte" HeaderText="Texte" SortExpression="Prod_Texte" /> <asp:BoundField DataField="Prod_Prix" DataFormatString="{0:C}" HeaderText="Prix" SortExpression="Prod_Prix" /> <asp:BoundField DataField="Prod_Image" HeaderText="Nom de l'Image" SortExpression="Prod_Image" /> <asp:TemplateField HeaderText="Catégorie" SortExpression="Cat_Id"> <EditItemTemplate> <asp:SqlDataSource ID="dsCategorie" runat="server" ConnectionString="<%$ ConnectionStrings:csSigneEtSens %>" SelectCommand="SELECT [Cat_Id], [Cat_Nom] FROM [Categorie] ORDER BY [Cat_Nom]"></asp:SqlDataSource> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsCategorie" DataTextField="Cat_Nom" DataValueField="Cat_Id" SelectedValue='<%# Bind("Cat_Id") %>' AppendDataBoundItems="True"> </asp:DropDownList> <div id="linkbuttons"> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" onclick="LinkButton1_Click">Ajouter</asp:LinkButton> | <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" onclick="LinkButton2_Click">Modifier</asp:LinkButton> | <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" onclick="LinkButton3_Click">Supprimer</asp:LinkButton> </div> <div id="text"> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" CausesValidation="false"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Valider" onclick="Button1_Click" /> <asp:Button ID="Button3" runat="server" Text="Valider" CausesValidation="false" onclick="Button3_Click"/> <asp:Button ID="Button2" runat="server" Text="Annuler" CausesValidation="false" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"> </asp:RequiredFieldValidator> </div> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Cat_Id") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Cat_Nom") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> </Fields> </asp:DetailsView>
Merci d'avance pour votre aide.
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 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("csSigneEtSens").ToString Dim Connection As SqlConnection = New SqlConnection(ConnectionString) Connection.Open() If Etat2.Mode = "Ajout" Then Dim Command As SqlCommand = New SqlCommand("INSERT INTO [Categorie] ([Cat_Nom]) VALUES (@Cat_Nom)") Command.Connection = Connection Command.CommandType = Data.CommandType.Text Command.Parameters.Add(New SqlParameter("@Cat_Nom", Data.SqlDbType.NVarChar)) Command.Parameters("@Cat_Nom").Value = CType(DetailsView1.FindControl("TextBox1"), TextBox).Text Command.ExecuteNonQuery() CType(DetailsView1.FindControl("DropDownList1"), DropDownList).DataBind() CType(DetailsView1.FindControl("Label1"), Label).ForeColor = Drawing.Color.Red CType(DetailsView1.FindControl("Label1"), Label).Text = "Catégorie ajoutée!" CType(DetailsView1.FindControl("TextBox1"), TextBox).Text = "" ElseIf Etat2.Mode = "Modification" Then Dim Command As SqlCommand = New SqlCommand("UPDATE [Categorie] SET [Cat_Nom] = @Cat_Nom WHERE [Cat_Id] = @Cat_Id") Command.Connection = Connection Command.CommandType = Data.CommandType.Text Command.Parameters.Add(New SqlParameter("@Cat_Nom", Data.SqlDbType.NVarChar)) Command.Parameters.Add(New SqlParameter("@Cat_Id", Data.SqlDbType.Int)) Command.Parameters("@Cat_Nom").Value = CType(DetailsView1.FindControl("TextBox1"), TextBox).Text Command.Parameters("@Cat_Id").Value = CType(DetailsView1.FindControl("DropDownList1"), DropDownList).SelectedValue Command.ExecuteNonQuery() CType(DetailsView1.FindControl("DropDownList1"), DropDownList).DataBind() CType(DetailsView1.FindControl("Label1"), Label).ForeColor = Drawing.Color.Red CType(DetailsView1.FindControl("Label1"), Label).Text = "Catégorie modifiée !" CType(DetailsView1.FindControl("TextBox1"), TextBox).Text = "" End If Connection.Close() End Sub
Partager