user1511029
user1511029

Reputation: 125

Why my submit button submit twice?

HTML Page

<body>
  <form id="form1" action="Default.aspx" method="post">
    <input runat="server" id="txtuser" type="text" />
    <input runat="server" id="txtpwd" type="password" />
    <input type="submit" value="Login"/>
 </form>
</body>

Code-behind

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
        Login();
   }
}

private void Login()
{
   if (checkUser(Request.Params["txtuser"],Request.Params["txtpwd"]))
   {
        Response.Redirect("Success.aspx");//if success
   }
}

I am developing a web page for old mobile version (like nokia N70) facing a problem. When I submit my username and password then check user return true to redirect to a new page. But it won't redirect to success.aspx. So I debug point on the Response.Redirect code, it can stop there and I continue run become error because getting the username&password null. Then I realized it loaded the page twice. How to solve it?

Upvotes: 2

Views: 1405

Answers (2)

Sani Huttunen
Sani Huttunen

Reputation: 24395

You want to login when there IS a PostBack. Not the other way around.

Change

if (!Page.IsPostBack)

to

if (Page.IsPostBack)

Make sure you have set AutoEventWireup to true in Code Front:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"...

otherwise Page_Load is never fired.

Upvotes: 3

slfan
slfan

Reputation: 9129

You should use Forms Authentication in a proper way, like explained here

You should do something like this for your redirect:

if (checkUser(userName.Text, password.Text))
{
    FormsAuthentication.RedirectFromLoginPage(userName.Text, false);
}

Upvotes: 2

Related Questions