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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
| using System;
using System.Data;
using System.Configuration;
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.Collections;
public partial class _Default : System.Web.UI.Page
{
private ArrayList list;
private ArrayList listBtLink;
private ArrayList listTxt;
private ArrayList listLbl;
private ArrayList listLblBlc;
private int nNbEl;//nombre de paramètres supplementaires
private int nNbCombo;//nombre de DropDownList supplementaires
private int nbBlc;
private Label Label5;
private String strRequetePrinc = ConfigurationManager.AppSettings["Requete"];
private String strRequetePrinTrans;
private DataControlField[] comb;
//private GridView GridView1;
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
listLblBlc = new ArrayList();
Label5 = new Label();
Label5.Text = " ";
nbBlc = 0;
// strRequetePrinc= this.SqlDataSource1.SelectCommand;
/*
* Recuperation du nom des colonnes de la gridview
* Chargement de cette liste dans la dropdownlist de depart si on est dans le cas
* du chargement initial de la page et non dans le cas d'un retour sur la page suite à une action
* de bouton de cette même page
*
* */
if (!Page.IsPostBack)
CreationGridView();
/*
* chargement de la GridView, on lui envoye la requette en fonction de l'action
* de l'utilisateur: filtrer, annuler filtre, changement de la page du gridView
* ou premier chargement de la page.
* Si on ne fait pas ca pour le changement de la page, la gridView affichera bien
* la 2e page mais avec la requete utilisé pour la construction du GridView
*
* */
if (HttpContext.Current.Session["requete"] != null && HddFldPstBck.Value.CompareTo("oui") == 0)
{
/*
* Si l'utilisateur a utiliser la fonction filtrer et qu'il souhaite changer
* de pages du GridView.
*
* */
this.SqlDataSource1.SelectCommand = (string)HttpContext.Current.Session["requete"];
}
else
{
this.SqlDataSource1.SelectCommand = strRequetePrinc;
}
/*
* Initialisation des divers éléments si on est dans le cas d'un premier chargement
* ou dans le cas d'une annulation de filtre
*
* */
if (HddFldEl.Value.CompareTo("")==0)
{
list = new ArrayList();
listBtLink = new ArrayList();
listTxt = new ArrayList();
listLbl = new ArrayList();
nNbEl = 0;
/*
* Creation d'une ligne de formulaire invisible, cela permet de la rendre actif
* si l'utilisateur souhaite un nouveau critère de recherche et cela,
* recharger la page
*
* */
CreationLigne(false);
//champs invisible qui contient le nombre de ligne de criteres suplementaires
this.HddFldEl.Value = "1";
/*
* Creation de variables de sessions pour stocker la structure de la page
* et ne pas la perdre à chaques rechargements
*
* */
HttpContext.Current.Session.Add("dropList", list);
HttpContext.Current.Session.Add("LinkButton", listBtLink);
HttpContext.Current.Session.Add("TextBox", listTxt);
HttpContext.Current.Session.Add("Label", listLbl);
}
else
{
//Chargement des données de la page
list = (ArrayList)HttpContext.Current.Session["dropList"];
listBtLink = (ArrayList)HttpContext.Current.Session["LinkButton"];
listTxt = (ArrayList)HttpContext.Current.Session["TextBox"];
listLbl = (ArrayList)HttpContext.Current.Session["Label"];
nNbCombo = 0;
nNbEl = 0;
/*
* Chargement des paramètres supplementaires. Le label nommé label5 permet
* de faire de la mise en forme. On ne peux afficher plusieurs fois le même
* éléments donc le tableau contenant les label5
*
* */
while ( nNbEl<int.Parse(this.HddFldEl.Value))
{
((Label)listLbl[nNbEl]).EnableViewState = false;
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
elementSup.Controls.Add((Label)listLbl[nNbEl]);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
elementSup.Controls.Add((DropDownList)list[nNbCombo]);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
((DropDownList)list[nNbCombo]).Items.Clear();
((DropDownList)list[nNbCombo]).Items.Add("(Choisir ...)");
for (i = 0; i < this.GridView1.Columns.Count; i++)
{
((DropDownList)list[nNbCombo]).Items.Add(this.GridView1.Columns[i].ToString().ToLower());//chargement de la combo
}
((DropDownList)list[nNbCombo]).EnableViewState = false;
nNbCombo++;
elementSup.Controls.Add((DropDownList)list[nNbCombo]);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
((DropDownList)list[nNbCombo]).Items.Clear();
((DropDownList)list[nNbCombo]).Items.Add("Contient");
((DropDownList)list[nNbCombo]).Items.Add("egal");
((DropDownList)list[nNbCombo]).EnableViewState = false;
nNbCombo++;
elementSup.Controls.Add((TextBox)listTxt[nNbEl]);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
elementSup.Controls.Add((DropDownList)list[nNbCombo]);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
((DropDownList)list[nNbCombo]).EnableViewState = false;
((DropDownList)list[nNbCombo]).Items.Clear();
((DropDownList)list[nNbCombo]).Items.Add("Et");
((DropDownList)list[nNbCombo]).Items.Add("Ou");
nNbCombo++;
elementSup.Controls.Add((LinkButton)listBtLink[nNbEl]);
//attribution des fonctions aux boutons
((LinkButton)listBtLink[nNbEl]).Click += new System.EventHandler(LinkButton1_ClickEfface);
((LinkButton)listBtLink[nNbEl]).Text = "Enlever cette propriété ...";
nNbEl++;
}
}
}
protected void rempliCombo(object sender, EventArgs e)
{
int i = 0,j=0;
if (!Page.IsPostBack)
{
this.DrpDownCol.Items.Clear();
this.DrpDownCol.Items.Add("(Choisir ...)");
for (i = 0; i < this.GridView1.Columns.Count; i++)
{
this.DrpDownCol.Items.Add(this.GridView1.Columns[i].ToString().ToLower());//chargement de la combo
}
}
if (HddFldEl.Value.CompareTo("") != 0)
{
while (j < int.Parse(this.HddFldEl.Value))
{
for (i = 0; i < this.GridView1.Columns.Count; i++)
{
((DropDownList)list[nNbCombo]).Items.Add(this.GridView1.Columns[i].ToString().ToLower());//chargement de la combo
}
j *= 3;
}
}
}
protected void CreationGridView()
{
this.SqlDataSource1.SelectCommand=strRequetePrinc;
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
/*
* Action du bouton Annuler Filtre
* Reinitialise la page.
* */
protected void Button1_Click(object sender, EventArgs e)
{
HddFldEl.Value = "";
this.TBxMots.Text = "";
this.GridView1.PageIndex = 0;
HddFldPstBck.Value = "";
Response.AppendHeader("Refresh", "0");
}
/*
* Action du bouton lien: Enlever cette propriété ...
* supprime la ligne correspondante
* */
protected void LinkButton1_ClickEfface(object sender, EventArgs e)
{
HddFldPstBck.Value = "oui";
LinkButton Btn;
int nPosLigneDel;
Btn = (LinkButton)sender;
if (nNbEl > 1)
{
nPosLigneDel = listBtLink.IndexOf(Btn);
/*
* Corrige le bug de la derniere ligne: lorsqu'on cliquait pour effacer
* l'avant derniere ligne puis qu'on cliquait pour effacer la derniere ligne.
* Le bouton renvoyé par le sender était celui de la ligne invisible(?!?).
* */
if (!Btn.Visible)
nPosLigneDel--;
((TextBox)listTxt[nPosLigneDel]).Text = "";
this.elementSup.Controls.Remove((Label)listLbl[nPosLigneDel]);
this.elementSup.Controls.Remove((DropDownList)list[nPosLigneDel * 3]);
this.elementSup.Controls.Remove((DropDownList)list[(nPosLigneDel * 3) + 1]);
this.elementSup.Controls.Remove((TextBox)listTxt[nPosLigneDel]);
this.elementSup.Controls.Remove((DropDownList)list[(nPosLigneDel * 3) + 2]);
this.elementSup.Controls.Remove((LinkButton)listBtLink[nPosLigneDel]);
list.RemoveAt(nPosLigneDel);
list.RemoveAt(nPosLigneDel);
list.RemoveAt(nPosLigneDel);
listBtLink.RemoveAt(nPosLigneDel);
listTxt.RemoveAt(nPosLigneDel);
listLbl.RemoveAt(nPosLigneDel);
nNbEl--;
this.nNbCombo -= 3;
HddFldEl.Value = nNbEl.ToString();
}
else
{
this.VisiDernLigne(false);
}
}
/*
* Action du bouton : Filtrer
* met à jour la GridView en changeant la requete associée et sauvegarde
* cette requete dans une variable de session
* */
protected void Button2_Click(object sender, EventArgs e)
{
int i;
Boolean bPrem=false;
String strWhere="";
for (i = 0; i < this.GridView1.Columns.Count; i++)
{
strWhere += this.GridView1.Columns[i].ToString() + " like '%" + this.TBxMots.Text + "%' ";
if (i < this.GridView1.Columns.Count - 1)
strWhere += " or ";
}
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans = strRequetePrinc +
" where (" + strWhere+")");
for (i = 0; i < nNbEl - 1; i++)
{
if (((TextBox)listTxt[i]).Text.CompareTo("") != 0 && ((DropDownList)list[i * 3]).SelectedValue.CompareTo("(Choisir ...)")!=0)
{
//AjoutParamDrpDwnLst1(i,((TextBox)listTxt[i]));
AjoutParam(i,((DropDownList)list[i * 3 + 1]), ((DropDownList)list[i * 3]).SelectedValue.ToString(), ((TextBox)listTxt[i]));
bPrem = true;
}
}
if (this.DrpDownCol.SelectedValue.CompareTo("(Choisir ...)") != 0 && this.TxtChoix.Text.CompareTo("") != 0)
{
if (nNbEl > 1)
{
AjoutParam(nNbEl - 1, this.DropDownList2, DrpDownCol.SelectedValue.ToString(), TxtChoix);
bPrem = true;
}
else
{
AjoutParam(nNbEl - 1, this.DropDownList2, DrpDownCol.SelectedValue.ToString(), TxtChoix);
bPrem = true;
}
}
if (bPrem == true)
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans + ")");
}
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
HddFldPstBck.Value = "oui";
}
/*
*
* Creation d'une nouveau parametre de selection.
* cette ligne ne sera pas affichée mais sera active si on demande son affichage
*
* */
public void CreationLigne(Boolean bVisible)
{
DropDownList DropList = new DropDownList();
DropDownList DropList1 = new DropDownList();
DropDownList DropList2 = new DropDownList();
TextBox TxtBox = new TextBox();
Label Lbl = new Label();
LinkButton LinkBtt = new LinkButton();
DropList.EnableViewState = false;
DropList1.EnableViewState = false;
DropList2.EnableViewState = false;
TxtBox.EnableViewState = false;
Lbl.EnableViewState = false;
LinkBtt.EnableViewState = false;
Lbl.Text = "où la propriété ";
LinkBtt.EnableViewState = false;
list.Insert(nNbCombo, DropList);
nNbCombo++;
list.Insert(nNbCombo, DropList1);
nNbCombo++;
list.Insert(nNbCombo , DropList2);
nNbCombo++;
listTxt.Insert(nNbEl, TxtBox);
listLbl.Insert(nNbEl, Lbl);
listBtLink.Insert(nNbEl, LinkBtt);
this.elementSup.Controls.Add(Lbl);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
this.elementSup.Controls.Add(DropList);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
this.elementSup.Controls.Add(DropList1);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
this.elementSup.Controls.Add(TxtBox);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
this.elementSup.Controls.Add(DropList2);
Label5 = new Label();
Label5.Text = " ";
listLblBlc.Insert(nbBlc, Label5);
elementSup.Controls.Add((Label)listLblBlc[nbBlc]);
nbBlc++;
this.elementSup.Controls.Add((LinkButton)listBtLink[nNbEl]);
((LinkButton)listBtLink[nNbEl]).Click += new System.EventHandler(LinkButton1_ClickEfface);
((LinkButton)listBtLink[nNbEl]).Text = "Enlever cette propriété ...";
nNbEl++;
//On affiche pas la derniere ligne
VisiDernLigne(false);
}
/*
* Fonction lié au bouton lien: Ajouter une propriété ...
*
* Affiche la derniere ligne qui était invisible
* Creer une autre ligne invisible
* Met à jour le champs contenant le nombre d'élément
*
* */
protected void LinkButton1_Click1(object sender, EventArgs e)
{
VisiDernLigne(true);
CreationLigne(false);
HddFldEl.Value = nNbEl.ToString();
}
/*
* Fonction permettant de modifier la visibilite l'avant derniere ligne crée
*
* */
public void VisiDernLigne(Boolean bVisLigne)
{
((Label)listLbl[nNbEl - 1]).Visible = bVisLigne;
((DropDownList)list[nNbCombo - 3]).Visible = bVisLigne;
((DropDownList)list[nNbCombo - 2]).Visible = bVisLigne;
((TextBox)listTxt[nNbEl - 1]).Visible = bVisLigne;
((DropDownList)list[nNbCombo - 1]).Visible = bVisLigne;
((LinkButton)listBtLink[nNbEl - 1]).Visible = bVisLigne;
}
/*
* Fonction qui permet de parcourir les DropDownList pour construire la requete
*
* */
public void AjoutParam(int nIndex, DropDownList DDLOperat, String strDrpDwnLst1, TextBox txt)
{
switch (DDLOperat.SelectedValue)
{
case "Contient"://Contient
if (nIndex > 0 )
{
if (((DropDownList)list[(nIndex - 1) * 3 + 2]).SelectedValue.CompareTo("Et") == 0)//Et
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans +=
" and [" + strDrpDwnLst1 + "] like '%" + txt.Text + "%' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
else//Ou
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans += " or [" + strDrpDwnLst1
+ "] like '%" + txt.Text + "%' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
}
else
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans += " and ([" + strDrpDwnLst1 + "] like '%" + txt.Text + "%' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
break;
case "egal"://=
if (nIndex > 0)
{
if (((DropDownList)list[(nIndex - 1) * 3 + 2]).SelectedValue.CompareTo("Et") == 0)//Et
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans +=
" and [" + strDrpDwnLst1 + "] like '" + txt.Text + "' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
else//Ou
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans += " or [" + strDrpDwnLst1
+ "] like '" + txt.Text + "' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
}
else
{
HttpContext.Current.Session.Add("requete", this.SqlDataSource1.SelectCommand = strRequetePrinTrans += " and ([" + strDrpDwnLst1 + "] like '" + txt.Text + "' ");
this.GridView1.PageIndex = 2;
this.GridView1.PageIndex = 0;
}
break;
default:
break;
}
}
} |