Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
5177 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  CustomValidator et UserControl...

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

CustomValidator et UserControl...

n°1234940
Arjuna
Aircraft Ident.: F-MBSD
Posté le 31-10-2005 à 12:44:07  profilanswer
 

Alors...
 
J'ai une page qui contient un UserControl "SearchBar".
Dans ce UserControl, j'ai un CustomValidator qui affiche un message d'erreur si aucun critère de recherche n'a été rempli.
Et ça, ça marche.
 
Ensuite, dans ma page, je voudrais savoir si le UserControl a levé une erreur ou non et...
Ben là ça se gâte :/
 
Page.IsValid plante, car je n'ai pas validé la page depuis ma page mais le UserControl
SearchBar.IsValid est toujours à True !!! (même s'il la contrôle est en erreur)
SearchBar.CustomValidator1.IsValid est aussi toujours à True :/
 
SearchBar.ascx.cs :

Code :
  1. namespace bci.Controls
  2. {
  3. using System;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.HtmlControls;
  9. /// <summary>
  10. ///  Summary description for SearchBar.
  11. /// </summary>
  12. public class SearchBar : System.Web.UI.UserControl
  13. {
  14.  public System.Web.UI.WebControls.TextBox txtCodpro;
  15.  protected System.Web.UI.WebControls.Label Label1;
  16.  protected System.Web.UI.WebControls.Label Label2;
  17.  public System.Web.UI.WebControls.TextBox txtNompro;
  18.  public System.Web.UI.WebControls.CustomValidator CustomValidator1;
  19.  protected System.Web.UI.WebControls.Button Button1;
  20.  private void Page_Load(object sender, System.EventArgs e)
  21.  {
  22.   // Put user code to initialize the page here
  23.  }
  24.  #region Web Form Designer generated code
  25.  override protected void OnInit(EventArgs e)
  26.  {
  27.   //
  28.   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  29.   //
  30.   InitializeComponent();
  31.   base.OnInit(e);
  32.  }
  33.  /// <summary>
  34.  ///  Required method for Designer support - do not modify
  35.  ///  the contents of this method with the code editor.
  36.  /// </summary>
  37.  private void InitializeComponent()
  38.  {
  39.   this.Load += new System.EventHandler(this.Page_Load);
  40.   this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
  41.  }
  42.  #endregion
  43.  private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
  44.  {
  45.   if (this.txtCodpro.Text.Trim() == "" && this.txtNompro.Text.Trim() == "" )
  46.   {
  47.    args.IsValid = false;
  48.   }
  49.   else
  50.   {
  51.    args.IsValid = true;
  52.   }
  53.  }
  54. }
  55. }


 
Ma page :

Code :
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace bci
  12. {
  13. /// <summary>
  14. /// Summary description for Collection.
  15. /// </summary>
  16. public class Collection : System.Web.UI.Page
  17. {
  18.  protected Controls.SearchBar SearchBar1;
  19.  private void Page_Load(object sender, System.EventArgs e)
  20.  {
  21.   // Ajouter le code pour bloquer si Session vide !
  22.   if (Session.Count == 0 || Session["login"] == null || Session["typtie"] == null || Session["codsoc"] == null)
  23.   {
  24.    Session.Clear();
  25.    Response.Redirect("Default.aspx", true);
  26.   }
  27.   Response.Write(((Decimal) Session["codsoc"]).ToString() + " - " + ((string) Session["typtie"]) + " - " + ((string) Session["login"]));
  28.   // On vérifie que les critères de recherche on bien été remplis
  29.   if (Page.IsPostBack)
  30.   {
  31.   Response.Write(SearchBar1.CustomValidator1.IsValid);
  32.   // if (Page.IsValid)
  33.   // {
  34.     if (SearchBar1.txtCodpro.Text.Trim() != "" )
  35.     {
  36.      Response.Write("Codpro est rempli" );
  37.     }
  38.     if (SearchBar1.txtNompro.Text.Trim() != "" )
  39.     {
  40.      Response.Write("Nompro est rempli" );
  41.     }
  42.     if (SearchBar1.txtCodpro.Text.Trim() == "" && SearchBar1.txtNompro.Text.Trim() == "" )
  43.     {
  44.      Response.Write("J'ai rien à foutre là..." );
  45.     }
  46.   // }
  47.   }
  48.  }
  49.  #region Web Form Designer generated code
  50.  override protected void OnInit(EventArgs e)
  51.  {
  52.   //
  53.   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  54.   //
  55.   InitializeComponent();
  56.   base.OnInit(e);
  57.  }
  58.  /// <summary>
  59.  /// Required method for Designer support - do not modify
  60.  /// the contents of this method with the code editor.
  61.  /// </summary>
  62.  private void InitializeComponent()
  63.  {   
  64.   this.Load += new System.EventHandler(this.Page_Load);
  65.  }
  66.  #endregion
  67. }
  68. }


 
:sweat:
 
Pkoi ça marche pas ? :cry:

mood
Publicité
Posté le 31-10-2005 à 12:44:07  profilanswer
 

n°1234942
Arjuna
Aircraft Ident.: F-MBSD
Posté le 31-10-2005 à 12:49:28  profilanswer
 

Pffff... Trouvé, faut le valider deux fois ce con :o
 

Code :
  1. SearchBar1.CustomValidator1.Validate();
  2. Response.Write(SearchBar1.CustomValidator1.IsValid);


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  CustomValidator et UserControl...

 

Sujets relatifs
[C# & ASP.NET] Lien depuis UserControl vers FormX ???récup évènements de usercontrol créés dynamiquement
comment insérer un usercontrol depuis le codebehind[C#] UserControl, enum et Resources
[activeX] Comment récupérer la propriété width d'un usercontrol ?Créer un userControl editable
No public UserControl detected[C#] Créer un UserControl à partir d'une classe
Plus de sujets relatifs à : CustomValidator et UserControl...


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)