Bonjour !
Voici quelques jours que je tâtonne pour trouver une solution à mon problème.
Concrètement, j'ai un Gridview dont le DataSource est un DataSet. J'ai mis en place un bouton "Modifier" pour pouvoir changer les champs d'une ligne.
Lors de cette édition de ligne, j'ai mis des TextBox et 2 DropDownList avec en SelectedValue la valeur de la cellule.
Mon problème est simple, quand je clique sur "Mettre à jour" après avoir modifié les valeurs des cellules, je rentre dans la méthode OnRowUpdating="GridClient_Updating" ; Ici j'essaye de récupérer les valeurs modifiées pour mettre à jour ma base de données. Mais je ne récupère que les anciennes valeurs (ce serait trop facile sinon).
Voici donc du code :
Page ASP.net, le GridView :
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 <asp:GridView runat="server" ID="GridClients" SkinID="CustomGridSansPage" Width="1100px" DataKeyNames="Identifiant" PageSize="15" AllowSorting="True" AutoGenerateColumns="False" OnSorting="GridClients_Sorting" OnRowUpdating="GridClient_Updating" OnRowEditing="GridClient_RowEditing" OnRowCancelingEdit="GridClient_RowCancelingEditing"> <Columns> <asp:TemplateField HeaderText="Identifiant" SortExpression="Identifiant"> <ItemTemplate> <asp:HyperLink ID="lienIdentifiant" NavigateUrl='<%#"~/Formulaires/DetailsClient.aspx?idClient=" + Eval("Identifiant")%>' runat="server" Text='<%#Eval("Identifiant")%>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="SIREN" SortExpression="SIREN"> <ItemTemplate> <asp:Label ID="lblSiren" runat="server" Text='<%#Eval("SIREN")%>' /></ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Libellé" SortExpression="Libellé"> <ItemTemplate> <asp:Label ID="lblLibelle" runat="server" Text='<%#Eval("Libellé")%>' /></ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Segment Risque" SortExpression="Segment Risque"> <ItemTemplate> <asp:Label ID="lblSegmentRisque" runat="server" Text='<%#Eval("Segment Risque")%>' /></ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Exclusion" SortExpression="Exclusion"> <ItemTemplate> <asp:CheckBox ID="checkboxExclusion" Checked='<%#Eval("Exclusion")%>' runat="server" Enabled="false" /> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="checkboxExclusionEdition" Checked='<%#Eval("Exclusion")%>' runat="server" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Motif Exclusion" SortExpression="Motif Exclusion"> <ItemTemplate> <asp:Label ID="labelMotif" runat="server" Text='<%#Eval("Motif Exclusion")%>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DropDownListMotifEdition" runat="server" SelectedValue='<%#Eval("Motif Exclusion")%>'> <asp:ListItem Text="" Value="" /> <asp:ListItem Text="Moteur de Renotation" Value="Moteur de Renotation" /> <asp:ListItem Text="Douteux" Value="DOUTEUX" /> <asp:ListItem Text="Pas d'encours vivant" Value="Pas d'encours vivant" /> <asp:ListItem Text="Changement de Moteur" Value="Changement de Moteur" /> <asp:ListItem Text="Autres" Value="Autres" /> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Moteur Renotation" SortExpression="Moteur Renotation"> <ItemTemplate> <asp:Label ID="labelMoteur" runat="server" Text='<%#Eval("Moteur Renotation")%>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DropDownListMoteurEdition" runat="server" DataSourceID="SqlDataSourceMoteur" DataTextField="NOM_MOTR_RNOTT" DataValueField="NOM_MOTR_RNOTT" AppendDataBoundItems="true" SelectedValue='<%#Eval("Moteur Renotation")%>'> <asp:ListItem Text="" Value="" /> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Portefeuille" SortExpression="Portefeuille"> <ItemTemplate> <asp:Label ID="labelPortefeuille" runat="server" Text='<%#Eval("Portefeuille")%>' /> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="PortefeuilleEdition" runat="server" Text='<%#Eval("Portefeuille")%>' /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Réception Justificatifs" SortExpression="Réception Justificatifs"> <ItemTemplate> <asp:CheckBox ID="checkboxReception" Checked='<%#Eval("Réception Justificatifs")%>' runat="server" Enabled="false" /> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="checkboxReceptionEdition" Checked='<%#Eval("Réception Justificatifs")%>' runat="server" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Date Reception" SortExpression="Date Réception"> <ItemTemplate> <asp:Label ID="labelDateReception" runat="server" Text='<%#Eval("Date Réception")%>' /> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="DateReceptionEdition" runat="server" Text='<%#Eval("Date Réception")%>' /> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Columns> </asp:GridView>
Et le code behind :
Merci de votre aide, je commence à péter une durite ^^'
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 namespace CampagneRenotation.Web.Formulaires { public partial class ListeClients : System.Web.UI.Page { private Manager monManager = new Manager(); private DataSet monDataSet = new DataSet(); protected void Page_Load(object sender, EventArgs e) { try { if (!monManager.IsCampagneVide()) { if (!IsPostBack) RemplissageGridClients(); } } catch (CustomException) { Response.Redirect(string.Format("~/PageErreur.aspx?erreur={0}", "Echec de la connexion à la base de données")); } } protected void btnTrier_Click(object sender, EventArgs e) { RemplissageGridClients(); } private void RemplissageGridClients() { string idFiliere = DropDownListFiliere.SelectedValue; string idMoteur = DropDownListMoteur.SelectedValue; string role = DropDownListRole.SelectedValue; string siren = txtSIREN.Text; string exclusion = DropDownListExclusion.SelectedValue; string reception = DropDownListReception.SelectedValue; monDataSet = monManager.GetDataSetRequeteListClient(idFiliere, idMoteur, role, siren, exclusion, reception); GridClients.DataSource = monDataSet; GridClients.DataBind(); } protected void GridClient_Updating(Object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridClients.Rows[e.RowIndex]; string identifiant = ((HyperLink)row.FindControl("lienIdentifiant")).Text; string exclusion = ((CheckBox)row.FindControl("checkboxExclusionEdition")).Checked.ToString(); DropDownList ddlMotif = (DropDownList)row.Cells[5].FindControl("DropDownListMotifEdition"); DropDownList ddlMoteur = (DropDownList)row.Cells[6].FindControl("DropDownListMoteurEdition"); string motifExclusion = ddlMotif.SelectedItem.Value; string moteurRenotation = ddlMoteur.SelectedItem.Value; string portefeuille = ((TextBox)row.FindControl("PortefeuilleEdition")).Text; string reception = ((CheckBox)row.FindControl("checkboxReceptionEdition")).Checked.ToString(); string dateReception = ((TextBox)row.FindControl("DateReceptionEdition")).Text; GridClients.EditIndex = -1; GridClients.DataBind(); try { Alert.Show(monManager.MiseAJourClient(identifiant, portefeuille, moteurRenotation, exclusion, motifExclusion, reception, dateReception).ToString()); } catch (CustomException ce) { Alert.Show(ce.Message); } } public void GridClient_RowEditing(Object sender, GridViewEditEventArgs e) { GridClients.EditIndex = e.NewEditIndex; GridClients.DataBind(); } public void GridClient_RowCancelingEditing(Object sender, GridViewCancelEditEventArgs e) { GridClients.EditIndex = -1; GridClients.DataBind(); } protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridClients.PageIndex = e.NewPageIndex; GridClients.DataBind(); } private const string ASCENDING = " ASC"; private const string DESCENDING = " DESC"; protected void GridClients_Sorting(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView(sortExpression, DESCENDING); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView(sortExpression, ASCENDING); } } public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } private void SortGridView(string sortExpression, string direction) { DataTable dt = monDataSet.Tables[0]; DataView dv = new DataView(dt); dv.Sort = sortExpression + direction; GridClients.DataSource = dv; GridClients.DataBind(); } } }
Partager