Monday, March 12, 2012

would like to create script which check if login.Text (from textbox "login") equals "szmit

I would like to create script which check if login.Text (from textbox "login") equals "szmitek" and passwod.Text equals "password". I have done it in this way:

protected

staticvoid Main(string[] args)

{

string clogin ="szmitek";string cpassword ="password";

}

protectedvoid SubmitBtn_Click(object sender,EventArgs e)

{

if (HttpUtility.HtmlEncode(login.Text) =(clogin))if (HttpUtility.HtmlEncode(password.Text) =(cpassword))

{

Response.Redirect(

"admin.aspx");

}

else {

Response.Redirect(

"fail.aspx");

}

else {

Response.Redirect(

"fail.aspx");

}

}

But it is incorrect (error: "The name 'clogin' does not exist in the current context"). Why? How should I compare variable "clogin" with value of textbox "login"?

Whole script code:

<%

@dotnet.itags.org.PageLanguage="c#"%>

<

SCRIPTrunat="server"language="c#">

protectedstaticvoid Main(string[] args)

{

string clogin ="szmitek";string cpassword ="password";

}

protectedvoid SubmitBtn_Click(object sender,EventArgs e)

{

if (HttpUtility.HtmlEncode(login.Text) = (clogin))if (HttpUtility.HtmlEncode(password.Text) = (cpassword))

{

Response.Redirect(

"admin.aspx");

}

else {

Response.Redirect(

"fail.aspx");

}

else {

Response.Redirect(

"fail.aspx");

}

}

</

SCRIPT>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

head>

<

metahttp-equiv="Content-Type"content="text/html; charset=iso-8859-2"/>

<

title>Administracja - logowanie</title>

<

linkrel="stylesheet"type="text/css"href="../format.css"/>

</

head>

<

body>

<

h1>Zaloguj si?</h1><divclass="center"><formrunat="server"language="c#"method="post">

Login:

<asp:TextBoxID="login"runat="server"></asp:TextBox><br/>

Has?o:

<asp:TextBoxID="password"runat="server"TextMode="Password"></asp:TextBox><br/><asp:ButtonID="Button1"runat="server"Text="Zaloguj si?"/></form></div>

</

body>

</

html>

your variables clogin and cpassword are scoped to be accessible only inside of your main function.

move their declaration outside of any function so that all functions can access them.


In the first place, why is there astatic Main(string[] args)function in the first place? As far as I know, that function will never run. ASP.NET runs differently than a console application. I won't get into too much detail, there's plenty out there on the internet. However, this is how you can fix your problem

<%@. Page Language="c#"%><SCRIPT runat="server" language="c#">private string clogin;private string cpassword; protected void Page_Load(object sender, EventArgs e){clogin = "szmitek";cpassword = "password";} // Rest of the code.</SCRIPT>

And how to compare Login.Text with clogin? I would like to create function which check ifHttpUtility.HtmlEncode(login.Text)equals "szmitek". Why:

protected void Page_Load(object sender, EventArgs e)
{

clogin = "szmitek";

cpassword = "password";

}

Developer Server find my variables but according to itif (HttpUtility.HtmlEncode(login.Text) =(clogin)) is incorrect. According to server my variables are defined but they aren't being used. What is wrong inif (HttpUtility.HtmlEncode(login.Text) =(clogin)).How to compare variable with text received from form?


Alright well there's a few problems with this...

If you've ever written in C# before you should know to use == not = for a comparison. Currently what is inside the if statement means do the assignment not the comparison.

However to retrieve the data and use it in a comparison just do the following (inside your event handler in this case):

if (login.Text == clogin && password.Text == cpassword) {// Insert the remainder of the code}
That should perform your comparison properly.

0 comments:

Post a Comment