Reputation: 45
I try to recover what users enter into textbox.
I create accessors to make my textboxes public because I've got problems of accessibility before.
Here's my full aspx.cs for the connexion :
public partial class ConnexionSaisieHeures : System.Web.UI.Page
{
/*Accesseurs*/
public string NomUtilisateur
{
get
{
return txtNomUtilisateur.Text;
}
set
{
txtNomUtilisateur.Text = value;
}
}
public string MotDePasse
{
get
{
return txtMotDePasse.Text;
}
set
{
txtMotDePasse.Text = value;
}
}
/*Événements*/
protected void Page_Load(object sender, EventArgs e)
{
ErreurConnexion.Visible = false;
lblErreurConnexion.Text = "";
}
protected void btnConnexion_Click(object sender, EventArgs e)
{
if (Authentifier(txtNomUtilisateur.Text, txtMotDePasse.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtNomUtilisateur.Text, false);
}
else
{
ErreurConnexion.Visible = true;
lblErreurConnexion.ForeColor = System.Drawing.Color.Red;
lblErreurConnexion.Text = "Erreur d'authentification : Le nom d'utilisateur ou le mot de passe est incorrect.";
}
}
/*Méthodes*/
/// <summary>
/// Méthode permettant l'authentification des utilisateurs à l'application
/// </summary>
/// <param name="strNomUtilisateur">Nom de l'utilisateur saisi</param>
/// <param name="strMotDePasse">Mot de passe saisi</param>
/// <returns>Booléen vérifiant si l'authentification a été faite ou non</returns>
private bool Authentifier(string strNomUtilisateur, string strMotDePasse)
{
bool bOk = false;
// Cryptage du mot de passe
strMotDePasse = FormsAuthentication.HashPasswordForStoringInConfigFile(strMotDePasse, "MD5");
// Création d'une connexion SGBD
SqlConnection oConnexion = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["SaisieHeuresConnectionString"]));
// Définition de la requête à exécuter
SqlCommand oCommand = new SqlCommand("SELECT * FROM Utilisateurs WHERE NomUtilisateur='" + strNomUtilisateur + "'", oConnexion);
try
{
// Ouverture de la connexion et exécution de la requête
oConnexion.Open();
SqlDataReader drUtilisateur = oCommand.ExecuteReader();
// Parcours de la liste des utilisateurs
while (drUtilisateur.Read())
{
if (drUtilisateur["MotDePasse"].ToString() == strMotDePasse)
{
bOk = true;
break;
}
}
}
catch
{
bOk = false;
}
oConnexion.Close();
return bOk;
}
}
Here's the part on the second form for having the first and last name of the user thanks to the username and the password :
private static ConnexionSaisieHeures WebFormConnexionSaisieHeures = new ConnexionSaisieHeures();
private void PrenomNomUtilisateur()
{
SqlConnection oConnexion = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["SaisieHeuresConnectionString"]));
SqlCommand oCommand = new SqlCommand("SELECT * FROM Utilisateurs WHERE NomUtilisateur='" + WebFormConnexionSaisieHeures.NomUtilisateur + "'", oConnexion);
try
{
// Ouverture de la connexion et exécution de la requête
oConnexion.Open();
SqlDataReader drUtilisateur = oCommand.ExecuteReader();
// Parcours de la liste des utilisateurs
while (drUtilisateur.Read())
{
if (drUtilisateur["MotDePasse"].ToString() == WebFormConnexionSaisieHeures.MotDePasse)
{
PrenomNom = drUtilisateur["PrenomNom"].ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception : " + ex.Message);
}
oConnexion.Close();
}
The aspx of my first webform :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConnexionSaisieHeures.aspx.cs" Inherits="SaisieHeures.ConnexionSaisieHeures" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WEATHER MEASURES : Connexion à l'application de saisie des heures</title>
<link href="SaisieHeures.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
</head>
<body>
<header>
Saisie des heures - Connexion
</header>
<br />
<article>
<form id="FormConnexionSaisieHeures" runat="server">
<table id="ErreurConnexion" runat="server">
<tr>
<td>
<asp:Label ID="lblErreurConnexion" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
<br />
<table id="Connexion">
<tr>
<td>
<asp:Label ID="lblNomUtilisateur" runat="server" Text="Nom d'utilisateur :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtNomUtilisateur" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMotDePasse" runat="server" Text="Mot de passe :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtMotDePasse" runat="server" TextMode="Password" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnConnexion" runat="server" Text="Connexion" OnClick="btnConnexion_Click" />
</td>
</tr>
</table>
</form>
</article>
</body>
</html>
The aspx of my second webform :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SaisieHeures.aspx.cs" Inherits="SaisieHeures.SaisieHeures" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WEATHER MEASURES : Application de saisie des heures</title>
<link href="SaisieHeures.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
</head>
<body>
<header>
Saisie des heures - Application
</header>
<br />
<article>
<form id="FormSaisieHeures" runat="server">
<table id="PrenomNomUtilisateur">
<tr>
<td colspan="2">
<asp:Label ID="lblPrenomNomUtilisateur" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<table id="SelectionMoisAnnee">
<tr>
<td>
<asp:Button ID="btnMoisPrecedent" runat="server" Text="<" OnClick="btnMoisPrecedent_Click" />
<asp:Label ID="lblMoisAnnee" runat="server"></asp:Label>
<asp:Button ID="btnMoisSuivant" runat="server" Text=">" OnClick="btnMoisSuivant_Click" />
</td>
</tr>
</table>
<br />
<table id="ErreurSaisie" runat="server">
<tr>
<td>
<asp:Label ID="lblErreurSaisie" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<table id="FicheSaisieHeures">
<tr>
<td>
<asp:Label ID="lblChoixSemaine" runat="server" Text="Semaine : "></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlSemaines" runat="server" OnSelectedIndexChanged="ddlSemaines_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblJours" runat="server" Text="Jours de la semaine"></asp:Label>
</td>
<td>
<asp:Label ID="lblNombreHeuresRealise" runat="server" Text="Nombre d'heures réalisées"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLundi" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtHeuresLundi" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMardi" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtHeuresMardi" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMercredi" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtHeuresMercredi" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblJeudi" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtHeuresJeudi" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblVendredi" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtHeuresVendredi" runat="server" Style="text-align: center"></asp:TextBox>
</td>
</tr>
</table>
<br />
<table id="MessagesCalculTotalHeuresMois" runat="server">
<tr>
<td>
<asp:Label ID="lblMessageCalculTotalHeuresMois" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<table id="TotalHeuresMois">
<tr>
<td>
<asp:Label ID="lblTotalHeuresMois" runat="server" Text="Nombre total d'heures réalisées dans le mois"></asp:Label>
<br />
<asp:TextBox ID="txtCalculTotalHeuresMois" runat="server" ReadOnly="True" Style="text-align: center"></asp:TextBox>
</td>
</tr>
</table>
<br />
<table id="Boutons">
<tr>
<td>
<asp:Button ID="btnValider" runat="server" Text="Valider" OnClick="btnValider_Click" />
</td>
<td>
<asp:Button ID="btnAnnuler" runat="server" Text="Annuler" OnClick="btnAnnuler_Click" />
</td>
<td>
<asp:Button ID="btnGenererExcel" runat="server" Text="Générer Excel" OnClick="btnGenererExcel_Click" />
</td>
<td>
<asp:Button ID="btnDeconnexion" runat="server" Text="Déconnexion" OnClick="btnDeconnexion_Click" />
</td>
</tr>
</table>
</form>
</article>
</body>
</html>
txtNomUtilisateur.Text
is null
; it never recover the text entered by the user.
How can i solve it, please ?
(First Reedit with full code, i can had more informations i you need, i really want to solve it)
P.S. : @ElekGuidolin I reput the original code before your propositions to show you what i've originaly done.
Upvotes: 2
Views: 1940
Reputation: 527
Christopher. Sorry about the confusion. But in fact I would like to improve the answer to a way better way to explain this.
When you are using Asp.Net Web Forms, you can get the value of your textbox with or without Request.Form, but only after a postback. If you want to get the value from Request.Form, you will need to put inside the quotation marks the entire name of the field, which, if is an asp.Net control, it will be something like this: ctl00$FeaturedContent$txtMyTest. But you can also get the value as the way you posted at first.
So, both ways work, but only after the postback, ok ?
If you are thinking in while typing the label will change together, you are talking about client script. And that is a question to another post and another subject, right ?!
public string TestProperty
{
get
{
//return txtMyTest.Text;
return Request.Form["ctl00$FeaturedContent$txtMyTest"];
}
set
{
txtMyTest.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
lblMyTest.Text = TestProperty;
}
}
EDIT:
@ChristopherLEBAS, since as you are creating a new ConnexionSaisieHeures in your second Form, the values in the second will always be null, because you are creating them in that moment (private static ConnexionSaisieHeures WebFormConnexionSaisieHeures = new ConnexionSaisieHeures();), and not using the existent filled form.
As you want to have this information in other page, maybe the better way is to use either Session or Cookies, because you need to maintain the state between pages, so you can do something like this:
protected void btnConnexion_Click(object sender, EventArgs e)
{
Session["NomUtilisateur"] = txtNomUtilisateur.Text;
Session["MotDePasse"] = FormsAuthentication.HashPasswordForStoringInConfigFile(txtMotDePasse.Text, "MD5");
if (Authentifier(txtNomUtilisateur.Text, txtMotDePasse.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtNomUtilisateur.Text, false);
}
else
{
ErreurConnexion.Visible = true;
lblErreurConnexion.ForeColor = System.Drawing.Color.Red;
lblErreurConnexion.Text = "Erreur d'authentification : Le nom d'utilisateur ou le mot de passe est incorrect.";
}
}
And then, in the second form:
private void PrenomNomUtilisateur()
{
string _NomUtilisateur = Session["NomUtilisateur"];
string _MotDePasse = Session["MotDePasse"];
SqlConnection oConnexion = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["SaisieHeuresConnectionString"]));
SqlCommand oCommand = new SqlCommand("SELECT * FROM Utilisateurs WHERE NomUtilisateur='" + _NomUtilisateur + "'", oConnexion);
try
{
// Ouverture de la connexion et exécution de la requête
oConnexion.Open();
SqlDataReader drUtilisateur = oCommand.ExecuteReader();
// Parcours de la liste des utilisateurs
while (drUtilisateur.Read())
{
if (drUtilisateur["MotDePasse"].ToString() == _MotDePasse)
{
PrenomNom = drUtilisateur["PrenomNom"].ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception : " + ex.Message);
}
finally
{
oConnexion.Close();
}
}
Note that I placed a Finally block, because if some error occurs, the connection will still be closed.
Upvotes: 1