Bonjour,

J'ai une fonction qui crée un Table dynamiquement, durant la création je rentre des valeurs dans les cellules.
Lorsque l'utilisateur click dans une cellule, une fonction Javascript change la valeur de cette cellule pour mettre un "1".

Mon problème est que lorsque je fait un postback, mes valeurs ne sont pas sauvegarder.
J'ai éssayer beaucoup de chose, les viewstate, session, etc...
Je n'ai réussi avec aucune d'entre elles.

Voila mon code-behind :

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
 
namespace MIS
{
    public partial class FeuilleCRA : System.Web.UI.Page
    {
        private int _startday;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetInterList();
            }
 
            Debug.WriteLine("Pageload");
 
        }
 
        private void GetInterList()
        {
            MISEntities MIS = new MISEntities();
 
            var query = from inter in MIS.Intervenant
                        join miss in MIS.Mission on inter.ID_Intervenant
                        equals miss.ID_Intervenant
                        orderby inter.Nom ascending
                        select new { 
                            nom = inter.Nom + " " + inter.Prenom,
                            id = inter.ID_Intervenant
                        };
 
            query = query.Distinct();
 
            DDlIntervenant.DataSource = query;
            DDlIntervenant.DataTextField = "nom";
            DDlIntervenant.DataValueField = "id";
            DDlIntervenant.DataBind();
        }
 
        protected int StartDay
        {
            get { return _startday; }
            set { _startday = value; }
        }
 
        private int GetDayCountForTable()
        {
            int nbDay = 0;
            DateTime dt = DateTime.Now;
            int CurrentMonth = int.Parse(DDlMonth.SelectedValue);
            int CurrentYear = dt.Year;
 
            DateTime DTstart = new DateTime(CurrentYear, CurrentMonth, 1);
 
            switch(DTstart.DayOfWeek.ToString())
            {
                case "Monday": StartDay = 0;
                    break;
                case "Tuesday": StartDay = 1;
                    break;
                case "Wednesday": StartDay = 2;
                    break;
                case "Thursday": StartDay = 3;
                    break;
                case "Friday": StartDay = 4;
                    break;
                case "Saturday": StartDay = 5;
                    break;
                case "Sunday": StartDay = 6;
                    break;
            }
 
            while(DTstart.Month == CurrentMonth){
                nbDay = DTstart.Day;
                DTstart = DTstart.AddDays(1);
            };
 
            return nbDay;
        }
 
        private void CreateDynamicTable(List<string> Lmission)
        { 
            List<string> day = new List<string>();   //liste des noms des jours
            day.Add("Lu");
            day.Add("Ma");
            day.Add("Me");
            day.Add("Je");
            day.Add("Ve");
            day.Add("Sa");
            day.Add("Di");
 
            //PlaceHolder1.Controls.Clear();    //vide le dernier tab
            List<int> lt = new List<int>(); //stock les colonne WE et jours ferier
            List<DateTime> Jferier = CalculeJourFerier(DateTime.Now.Year);
            string[] AjFerier = new string[Jferier.Count];
            int SelectedMonth = int.Parse(DDlMonth.SelectedValue);
            for (int i = 0; i < Jferier.Count; i++)
            {
                if (Jferier[i].Month == SelectedMonth)
                {
                    lt.Add(Jferier[i].Day);
                    AjFerier[i] = Jferier[i].Day.ToString();
                }
            }
 
            int nbMission = Lmission.Count;
            int tblRows = 12 + nbMission;                  // nombre de ligne -> passage en dynamique
            int tblCols = GetDayCountForTable() + 2;    // nombre de jours dans le mois + colonne total
            Session["Rows"] = tblRows.ToString();
 
            List<string> myStyle = new List<string>();
            myStyle.Add("Background-color:#e0ffff;Font-size:smaller");
            myStyle.Add("Background-color:#7abbd1;Font-size:smaller");
            myStyle.Add("Background-color:#cce2e2;Font-size:smaller");
            myStyle.Add("Background-color:#6ba5ba;Font-size:smaller");
            myStyle.Add("Background-color:#add8e6;Font-size:smaller");
            myStyle.Add("Background-color:#9ec6d3;Font-size:smaller");
            int colorSwitchDefault = 0;
            int colorSwitchWE = 0;
            int colorSwitchST = 0;
            JavaScriptSerializer js = new JavaScriptSerializer();
            string str = js.Serialize(AjFerier);
 
            int startingDay = StartDay;  //nom du 1er du mois
 
            bool boo = false;       // var ctrl
 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Height = 20;
 
                if (i % 2 == 0)
                {
                    colorSwitchDefault = 0;
                    colorSwitchWE = 1;
                    colorSwitchST = 4;
                }
                else
                {
                    colorSwitchDefault = 2;
                    colorSwitchWE = 3;
                    colorSwitchST = 5;
                }
 
                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();
                    tc.HorizontalAlign = HorizontalAlign.Center;
                    tc.VerticalAlign = VerticalAlign.Middle;
 
                    if (j == 0 && i > 1)    //cols 0 row > 1  (cells de noms de mission et de sous-total)
                    {
                        tc.Attributes.Add("width", "100px");
                        if (i <= nbMission + 1)  //nom de mission (cells name)
                        {
                            tc.Text = Lmission[i - 2];
                            tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                        }
                        else
                        {
                            if (i == nbMission + 2)
                                tc.Text = "Sous-Total"; //sous-total (cell name)}
                            if (i == nbMission + 3){
                                tc.Text = "Congés"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 4){
                                tc.Text = "RTT"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 5)
                            {
                                tc.Text = "Maladie"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 6)
                            {
                                tc.Text = "Formation"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 7)
                            {
                                tc.Text = "Intercontrat"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 8)
                            {
                                tc.Text = "Absence ex"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 9)
                            {
                                tc.Text = "Exclue"; tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                            }
                            if (i == nbMission + 10)
                            {
                                tc.Text = "Sous-Total"; 
                            }
                            if (i == nbMission + 11)
                            {
                                tc.Text = "Total / jour"; 
                            }
 
                        }
                    }
                    else
                    {
                        if (j != tblCols - 1 && j != 0 /*&& i != tblRows - 1*/ && i != (nbMission + 2) && i != (nbMission+10)) //toutes les colonnes sauf la 1 et la derniere + toute les ligne sans les sous-totaux
                        {
 
                            tc.Attributes.Add("width", "20px");
                            if (i == 0)
                            {
                                if (startingDay == 7)
                                    startingDay = 0;
 
                                tc.Text = day[startingDay];
 
                                for (int r = 0; r < lt.Count; r++)
                                {
                                    if (lt[r] == j)
                                      tc.Attributes.Add("style", myStyle[colorSwitchWE]);
                                }
 
                                if (startingDay == 5 || startingDay == 6)
                                {
                                    tc.Attributes.Add("style", myStyle[colorSwitchWE]);
                                    lt.Add(j);
                                }
 
                                startingDay++;
                            }
                            else
                            {
                                for (int v = 0; v < lt.Count; v++)
                                {
                                    if (j == lt[v])
                                    {
                                        tc.Attributes.Add("style", myStyle[colorSwitchWE]);
                                        boo = true;
                                        break;
                                    }
                                }
                                if (boo == false)
                                    tc.Attributes.Add("style", myStyle[colorSwitchDefault]);
                                boo = false;
 
                                if (i == 1)
                                {
                                    tc.Text = (j).ToString();
                                }
                                else
                                {
                                    tc.Text = "-";
                                    if (i != tblRows - 1)
                                    {
                                        tc.Attributes.Add("ondblclick", "totalclk(this," + str + ")");
                                        tc.Attributes.Add("onClick", "totaldblclk(this," + str + ")");
                                    }
                                }
                            }
 
                            if (i == tblRows - 1)
                            {
                                tc.Text = "0";
                            }
 
                        }
                        else
                        {
                            if (i == 1 && j == tblCols -1)
                                tc.Text = "Total";
                            else
                            {
                                if ((i == nbMission + 2 || i == nbMission + 10 ||i == 0) && j != tblCols - 1)
                                    tc.Attributes.Add("style", "Background-color:none");
                                else
                                {
                                    if ((i == tblRows - 1 || i == nbMission + 2 || i == nbMission + 10) && j == tblCols - 1)
                                    {
                                        tc.Attributes.Add("style", "Background-color:LightGreen;font-weight:bold;color:Black");
                                        tc.Text = "0";
                                    }
                                    else
                                    {
                                        if (j != 0 && i != 0)
                                        {
                                            tc.Attributes.Add("style", myStyle[colorSwitchST]);
                                            tc.Text = "0";
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }
            tbl.BackColor = System.Drawing.Color.LightGray;
 
            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not
            Session["table"] = tbl;
            ViewState["dynamictable"] = true;
            tbl.EnableViewState = true;
            Debug.WriteLine("create");
        }
 
        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            VerifAndGetMission();
        }
 
        private void VerifAndGetMission()
        {
            List<string> mission = new List<string>();
            MISEntities MIS = new MISEntities();
            int id = int.Parse(DDlIntervenant.SelectedValue);
 
            var query = from miss in MIS.Mission
                        where miss.ID_Intervenant == id
                        orderby miss.DateDebut ascending
                        select miss.NomProjet;
 
            mission = query.ToList<string>();
            Session["save"] = mission;
 
            CreateDynamicTable(mission);
        }
 
        protected void BSave_Click(object sender, EventArgs e)
        {
 
            int NbRows = int.Parse((string)Session["Rows"]) ;
            int NbCols = GetDayCountForTable() + 2;
            string Trame = string.Empty;
 
            for (int i = 2; i < NbRows; i++)
            {
                for (int j = 1; j < NbCols; j++)
                {
                    //Seulement les lignes de saisie (pas de totaux)
                    if (i != 3 && i != 11 && i != 12 && j != (NbCols - 1))
                    {
                        Trame += tbl.Rows[i].Cells[j].Text;
                    }
                }
            }
 
            test.InnerHtml = Trame;
        }
 
        protected void BForLate_Click(object sender, EventArgs e)
        {
 
        }
 
        private List<DateTime> CalculeJourFerier(int an)
        {
            DateTime JourAn = new DateTime(an, 1, 1);
            DateTime FeteTravail = new DateTime(an, 5, 1);
            DateTime Victoire1945 = new DateTime(an, 5, 8);
            DateTime FeteNational = new DateTime(an, 7, 14);
            DateTime Assomption = new DateTime(an, 8, 15);
            DateTime Toussaint = new DateTime(an, 11, 1);
            DateTime Armistice = new DateTime(an, 11, 11);
            DateTime Noel = new DateTime(an, 12, 25);
            double G = an % 19;
            double C = Math.Floor((double)an / 100);
            double H = (C - Math.Floor(C / 4) - Math.Floor((8 * C + 13) / 25) + 19 * G + 15) % 30;
            double I = H - Math.Floor(H / 28) * (1 - Math.Floor(H / 28) * Math.Floor(29 / (H + 1)) * Math.Floor((double)(21 - G) / 11));
            double J = (an * 1 + Math.Floor((double)an / 4) + I + 2 - C + Math.Floor(C / 4)) % 7;
            double L = I - J;
            int MoisPaques =(int)( 3 + Math.Floor((L + 40) / 44));
            int JourPaques = (int)(L + 28 - 31 * Math.Floor((double)MoisPaques / 4));
            DateTime Paques = new DateTime(an, MoisPaques - 1, JourPaques);
            DateTime VendrediSaint = new DateTime(an, MoisPaques, JourPaques);
            DateTime LundiPaques = new DateTime(an, MoisPaques, JourPaques + 1);
            DateTime Ascension = new DateTime(an, MoisPaques, JourPaques).AddDays(39);
            DateTime Pentecote = new DateTime(an, MoisPaques, JourPaques).AddDays(49);
            DateTime LundiPentecote = new DateTime(an, MoisPaques, JourPaques).AddDays(50); ;
 
            List<DateTime> JourFerier = new List<DateTime>();
            JourFerier.Add(JourAn);
            JourFerier.Add(FeteTravail);
            JourFerier.Add(Victoire1945);
            JourFerier.Add(Assomption);
            JourFerier.Add(FeteNational);
            JourFerier.Add(Toussaint);
            JourFerier.Add(Armistice);
            JourFerier.Add(Noel);
            JourFerier.Add(Paques);
            JourFerier.Add(VendrediSaint);
            JourFerier.Add(LundiPaques);
            JourFerier.Add(Ascension);
            JourFerier.Add(LundiPentecote);
 
            return JourFerier;
        }
 
        protected override object SaveViewState()
        {
            object[] newViewState = new object[2];
 
            List<string> txtValues = new List<string>();
 
           /* foreach (TableRow row in tbl.Controls)
            {
                foreach (TableCell cell in row.Controls)
                {
                    if (cell.Controls[0] is TextBox)
                    {
                        txtValues.Add(((TextBox)cell.Controls[0]).Text);
                    }
                }
            }*/
 
            int v = 0; 
 
            for (int i =  0; i < tbl.Rows.Count ; i++)
                for (int j = 0; j < tbl.Rows[0].Cells.Count; j++)
                {
                    txtValues.Add(tbl.Rows[i].Cells[j].Text);
                    Debug.WriteLine("fill" + " " + v + " " + txtValues[v]);
                    v++;
                }
 
            newViewState[0] = txtValues.ToArray();
            newViewState[1] = base.SaveViewState();
            return newViewState;
        }
 
        protected override void LoadViewState(object savedState)
        {
 
            //if we can identify the custom view state as defined in the override for SaveViewState
            if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
            {
                Debug.WriteLine("LoadView");
                object[] newViewState = (object[])savedState;
                string[] txtValues = (string[])(newViewState[0]);
                if (txtValues.Length > 0)
                {
                    //re-load tables
                    CreateDynamicTable((List<string>)Session["save"]);
                    int i = 0;
                 /*   foreach (TableRow row in tbl.Controls)
                    {
                        foreach (TableCell cell in row.Controls)
                        {
                            if (cell.Controls[0] is TextBox && i < txtValues.Length)
                            {
                                ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
 
                            }
                        }
                    }*/
 
                    for (int v = 0; v < tbl.Rows.Count; v++)
                        for (int j = 0; j < tbl.Rows[0].Cells.Count; j++)
                        {
                            tbl.Rows[v].Cells[j].Text = txtValues[i].ToString();
                            Debug.WriteLine("fill" +" " +i+" " +txtValues[i] );
                            i++;
                        }
 
                }
                //load the ViewState normally
                base.LoadViewState(newViewState[1]);
            }
            else
            {
                base.LoadViewState(savedState);
            }
        }
    }
}
Histoire d'être un peu plus clair, les fonctions principales sont :
CreateDynamicTable() // crée le tableau
BSave_Click() // Bouton qui doit sauvegarder mes valeurs
SaveViewState()
LoadViewState() // Les override avec quoi j'ai essayer de sauvegarder dans le viewState.

Le code Javascript pour changer les valeur des cellules est :
var1.innerHTML = "1"; //où var1 représente la cellule

Le table est créer dans la page aspx :
<asp:Table id="tbl" runat="server" ></asp:Table>

Je suis preneur de n'importe quel piste