سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top