Vous êtes sur la page 1sur 5

Asp.

net
Le contrôle image

imgDefault.ImageUrl = "Images/Pic.png";

Les contrôles de validation

Le champ nom est obligatoir :

<asp:RequiredFieldValidator

ID="RequiredFieldValidator1"runat="server"ControlToValidate="txtNom"
Display="Dynamic" ErrorMessage="le champ nom est obligatoir
!">*</asp:RequiredFieldValidator>

Validation du champ ID : doit être un multiple de 5

<asp:CustomValidator ID="CustomValidator1" runat="server"

ErrorMessage="ID doit être un multiple de 5 !"


ControlToValidate="txtID" Display="Dynamic"
ClientValidationFunction="IDValidationClient">!</asp:CustomValidator
>

Validation du champ jour de : la valeur doit être comprise ente 26/03/2012 et 31/03/2012

<asp:RangeValidator ID="RangeValidator1" runat="server"


ControlToValidate="txtJourDe" Display="Dynamic"
ErrorMessage="le jour n'est pas dans l'intervalle valide !"
MaximumValue="31/03/2012" MinimumValue="26/03/2012"
Type="Date">*</asp:RangeValidator>

Validation du champ âge : doit être supérieur ou égal = 18 ans

<asp:CompareValidator ID="CompareValidator1" runat="server"


ControlToValidate="txtAge" Display="Dynamic" ErrorMessage="Vous
devez avoir au moins 18 ans !" Operator="GreaterThanEqual"
Type="Integer" ValueToCompare="18">*</asp:CompareValidator>

Validation du champ date de naissance : la date doit être au format jj/mm/aaaa

<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ControlToValidate="txtDateNaissance"
ErrorMessage="Date de naissance invalide !"
ValidationExpression="^(((0[1-
9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6- 9]|[2-
9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-
9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-
9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)
(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
>!</asp:RegularExpressionValidator>

Validation du champ Email :

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtEmail" Display="Dynamic"
ErrorMessage="Le champ email n'est pas valide !"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-
.]\w+)*">!</asp:RegularExpressionValidator>

Validation du champ mot de passe : vérifier la correspondance entre le mot de passe


(txtMotPasse) et sa confirmation (txtRetaperMotPasse)

<asp:CompareValidator ID="CompareValidator2" runat="server"


ControlToCompare="txtMotPasse"
ControlToValidate="txtRetaperMotPasse" Display="Dynamic"
ErrorMessage="Les mots de passe ne correspondent
pas">!</asp:CompareValidator>

Passage par URL

//btnAffiche_Click

Response.Redirect("Default2.aspx?Nom=" +
Server.UrlEncode(txtNom.Text));

// Page_Load

lblNom.Text = Request.QueryString["Nom"];

Les Cookies

//btnAffiche_Click

Response.Cookies["MonCookie"].Value = txtNom.Text ;
Response.Cookies["MonCookie"].Expires = new DateTime(2012, 03, 24);
Response.Redirect("Defalut2.aspx");

// Page_Load

lblNom.Text = Request.Cookies["MonCookie"].Value ;

Les variables de Session

//Button1_Click

Session["TestVariable"] = TextBox1.Text;

// Button2_Click

TextBox2.Text = Session["TestVariable"].ToString();
Les variables d'application

//Button1_Click

Application.Lock();

Application["TestVariable"] = TextBox1.Text;

Application.UnLock();

// Button2_Click

TextBox2.Text = Application["TestVariable"].ToString();

public partial class authentification : System.Web.UI.Page

protected void Page_PreRender(object sender, EventArgs e)

{if (Response.Cookies["login"] != null){

TextBox1.Text = Request.Cookies["login"].Value;

if (Response.Cookies["pw"] != null){

if (Request.Cookies["login"].Value == "mail@gmail.com" && Request.Cookies["pw"].Value


== "123456"){

Response.Redirect("index.aspx");}}}}

protected void Button1_Click(object sender, EventArgs e)

{if (TextBox1.Text == "rabhi@gmail.com" && TextBox2.Text == "123456"){

Response.Cookies["login"].Value = TextBox1.Text;

Response.Cookies["login"].Expires = new DateTime(2016, 9, 26);

if (CheckBox1.Checked)

{Response.Cookies["pw"].Value = TextBox2.Text;

Response.Cookies["pw"].Expires = new DateTime(2016, 9, 26);}

Response.Redirect("index.aspx");}}}}

Remplire drop down liste


ddl.DataSource = dt ;
ddl.DataValueField = "Id_Categorie";
ddl.DataTextField = "Lib_Categorie";
ddl.DataBind();
Button connexion
cnx.Open();
cmd = new SqlCommand("select * from utilisateur where
login='"+TextBox1.Text+"' and pass='"+TextBox2.Text+"'", cnx);
dr=cmd.ExecuteReader();
if (dr.Read())
Response.Redirect("Accuile.aspx");
else Label3.Text = "login ou mot de passse incorrect";
service web
[WebMethod]
public double listeClient(string cin)
{
cnx.Open();
cmd = new SqlCommand("select sum(c.montant) from cheque c, Client ci where
c.cin=ci.CIN and ci.CIN="+cin, cnx);
double a = double.Parse(cmd.ExecuteScalar().ToString());
cnx.Close();
return a;

Le test de la page web

ListeClient c = new ListeClient();


GridView1.DataSource = c.listeClient(TextBox1.Text);
GridView1.DataBind();

FileUpload

FileUpload1.SaveAs("D:\\upload\\" + FileUpload1.FileName);

Grid View

Dons le événement Row commandes

int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;

Ajouter
if (e.CommandName == "ajouter")
{
cnx.Open();

cmd = new SqlCommand("insert into typeProduit values (@typeProduit ,@TVA)", cnx);


cmd.Parameters.Add("@typeProduit", SqlDbType.Char);
cmd.Parameters.Add("@TVA", SqlDbType.Int);
cmd.Parameters["@typeProduit"].Value =
((TextBox)gViewPrd.FooterRow.FindControl("textBFootertypeProduit")).Text;
cmd.Parameters["@TVA"].Value =
((TextBox)gViewPrd.FooterRow.FindControl("textBFooterTVA")).Text;
cmd.ExecuteNonQuery();
remlire();
}
cnx.Close();
}

}
Annuler
if (e.CommandName == "annuler")
{
gViewPrd.EditIndex = -1;
remlire();

}
supprimer
else if (e.CommandName == "DeletePrd")
{
cnx.Open();
SqlCommand cmdDelete = new SqlCommand("DELETE FROM typeProduit WHERE idtype =
@idtype", cnx);

cmdDelete.Parameters.Add("@idtype", SqlDbType.Int);

cmdDelete.Parameters["@idtype"].Value =
((Label)gViewPrd.Rows[rowIndex].FindControl("labelidtype")).Text;

cmdDelete.ExecuteNonQuery();
remlire();

modifier
if (e.CommandName == "modifier")
{
gViewPrd.EditIndex = rowIndex;
remlire();
}
Mise a jour
if (e.CommandName == "Update")
{
cnx.Open();
cmd = new SqlCommand(@"update typeProduit set typeProduit =
@typeProduit, TVA = @TVA where IdType = @IdType", cnx);

cmd.Parameters.Add("@IdType", SqlDbType.Int);
cmd.Parameters.Add("@typeProduit", SqlDbType.Char);
cmd.Parameters.Add("@TVA", SqlDbType.Int);

cmd.Parameters["@IdType"].Value =
((Label)gViewPrd.Rows[rowIndex].FindControl("labelidtype")).Text;
cmd.Parameters["@typeProduit"].Value =
((TextBox)gViewPrd.Rows[rowIndex].FindControl("textBtypeProduit")).Text;
cmd.Parameters["@TVA"].Value =
((TextBox)gViewPrd.Rows[rowIndex].FindControl("textBTVA")).Text;

cmd.ExecuteNonQuery();

gViewPrd.EditIndex = -1;
remlire();
}

Vous aimerez peut-être aussi