2015-01-06 100 views
0

我试图制作一个连接到数据库的ASP.NET基本站点。它应该允许用户注册并登录。按钮只能在第二次点击上工作

我使用javascript检查输入并在代码背后以防万一它被禁用。

问题是,只要我第一次点击注册,登录或注销按钮,他们将无法工作;该页面保持不变。 然而,他们第二次完美地工作。调试器说它被称为两次。

有什么想法?

ASP:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" 
Inherits="Register_and_Login.Main" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<script type="text/javascript"> 
    function isUserValid() { 
    var UserLength = document.getElementById("UserTB").value.length; 
    var ValidatorLabel = document.getElementById("ValidateUser"); 
    if (UserLength < 6 || UserLength > 15) { 
     ValidatorLabel.style.display = 'inline'; 
     return false; 
     } 
    else { 
     ValidatorLabel.style.display = 'none'; 
     return true; 
    } 
    } 
    function isPassValid() { 
     var PassLength = document.getElementById("PasswordTB").value.length; 
     var ValidatorLabel = document.getElementById("ValidatePassword"); 
     if (PassLength < 6 || PassLength > 15) { 
      ValidatorLabel.style.display = 'inline'; 
      return false; 
     } 
     else { 
      ValidatorLabel.style.display = 'none'; 
      return true; 
     } 
    } 
    function isConfirmValid() { 
     var Password = document.getElementById("PasswordTB").value; 
     var Me = document.getElementById("ConfirmTB").value; 
     var ValidatorLabel = document.getElementById("ValidateConfirm"); 
     if (Password == Me) { 
      ValidatorLabel.style.display = 'none'; 
      return true; 
     } 
     else { 
      ValidatorLabel.style.display = 'inline'; 
      return false; 
     } 
    } 
    function isEmailValid() { 
     var str = document.getElementById("EmailTB").value; 
     var lastAtPos = str.lastIndexOf('@'); 
     var lastDotPos = str.lastIndexOf('.'); 
     var isFine = (lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2); 
     var ValidationLabel=document.getElementById("ValidateEmail"); 
     if(isFine) 
     { 
      ValidationLabel.style.display='none'; 
      return true; 
     } 
     else 
     { 
      ValidationLabel.style.display='inline'; 
      return false; 
     } 
    } 
</script> 
<title></title> 
<style type="text/css"> 
    .Validators 
    { 
     display:none; 
    } 
</style> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <asp:Panel id="RegisterRelated" runat="server"> 
    Username:<br /> 
    <asp:TextBox ID="UserTB" runat="server" OnChange="isUserValid()" AutoPostBack="false"></asp:TextBox> 
    <asp:Label ID="ValidateUser" runat="server" ForeColor="Red" 
     Text="Username must be 6-15 characters in length, and contain no special characters." CssClass="Validators"></asp:Label> 
    <br /> 
    Password:<br /> 
    <asp:TextBox ID="PasswordTB" runat="server" OnChange="isPassValid()" AutoPostBack="false"></asp:TextBox> 
    <asp:Label ID="ValidatePassword" runat="server" ForeColor="Red" 
     Text="Password must be 6-15 characters in length, and contain no special characters." CssClass="Validators"></asp:Label> 
    <br /> 
    Confirm password:<br /> 
    <asp:TextBox ID="ConfirmTB" runat="server" OnChange="isConfirmValid()" AutoPostBack="false"></asp:TextBox> 
    <asp:Label ID="ValidateConfirm" runat="server" ForeColor="Red" 
     Text="This field must match the password field." CssClass="Validators"></asp:Label> 
    <br /> 
    Email:<br /> 
    <asp:TextBox ID="EmailTB" runat="server" OnChange="isEmailValid()" AutoPostBack="false"></asp:TextBox> 
    <asp:Label ID="ValidateEmail" runat="server" ForeColor="Red" Text="Invalid Email." CssClass="Validators"></asp:Label> 
    <br /> 
    <br /> 
    <asp:Button ID="Register" runat="server" Text="Register" onclick="Register_Click" EnableViewState="false"/> 
    <br /> 
    <asp:Panel ID="Answer" runat="server" > 
    </asp:Panel> 
    </asp:Panel> 
    <br /> 
    <br /> 
    <asp:Panel id="LoginRelated" runat="server"> 
    User: 
    <asp:TextBox ID="LoginUserTB" runat="server" AutoPostBack="false"></asp:TextBox> 
    <br /> 
    Password: 
    <asp:TextBox ID="LoginPassTB" runat="server" AutoPostBack="false"></asp:TextBox> 
     <br /> 
    <asp:Button ID="Login" runat="server" Text="Login" onclick="Login_Click" EnableViewState="false" /> 
    <br /> 
    </asp:Panel> 
    <asp:Panel ID="InPage" runat="server"> 
    <asp:Panel ID="LogAnswer" runat="server"> 
    </asp:Panel> 
    <br /> 
    <asp:Label ID="WelcomeTag" runat="server"></asp:Label> 

     <br /> 
     <br /> 
     <asp:Button ID="logout" runat="server" onclick="logout_Click" Text="Logout" EnableViewState="false"/> 

    </asp:Panel> 
</div> 
</form> 
</body> 
</html> 

C#登录,退出&注册按钮:

protected void Register_Click(object sender, EventArgs e) 
    { 
     Label Reply = new Label(); 
     if (Session["User"] == null) 
     { 
      Result myRegResult = Result.IN_PROG; 
      User myAddedUser = new User(UserTB.Text, PasswordTB.Text, EmailTB.Text); 
      DbManager.OpenDbConnection(); 
      myRegResult = DbManager.Register(myAddedUser); //Connection with the database. 
      Reply.Text = resultToString(myRegResult); 
      Reply.ForeColor = resultColor(myRegResult); 
     } 
     else 
     { 
      Reply.Text = "You must log out before you register."; 
      Reply.ForeColor = resultColor(Result.EXEC_ERROR); 
     } 
     Answer.Controls.Add((Control)Reply); 
     //Reset_Fields(); 
    } 


protected void Login_Click(object sender, EventArgs e) 
    { 
     Label Reply = new Label(); 
     LoginProc Status = LoginProc.IN_PROG; 
     DbManager.OpenDbConnection(); 
     Status = DbManager.Login(LoginUserTB.Text, LoginPassTB.Text); //Connection with the database 
     Reply.Text = ProcToString(Status); 
     Reply.ForeColor = ProcToColor(Status); 
     LogAnswer.Controls.Add(Reply); 
     if (Status == LoginProc.FINE) 
      Session["User"] = new User(LoginUserTB.Text, LoginPassTB.Text, null); 
     //Reset_Fields(); 
    } 



protected void logout_Click(object sender, EventArgs e) 
    { 
     Session["User"] = null; 

    } 

页面加载:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["User"] != null) 
     { 
      RegisterRelated.Visible = false; 
      LoginRelated.Visible = false; 
      InPage.Visible = true; 
      WelcomeTag.Text = "Welcome, " + ((User)Session["User"]).Username + "."; 
     } 
     else 
     { 
      RegisterRelated.Visible = true; 
      LoginRelated.Visible = true; 
      WelcomeTag.Text = String.Empty; 
      InPage.Visible = false; 
     } 
    } 

编辑:我的一个朋友劝我去看看在ASP.NET页面生命周期中,它可能与数据库交互有关在页面出现后完成,如果有帮助的话。

+0

我实在看不出这里有什么问题。我仍然使用您编写的代码创建了一个示例应用程序。但一切似乎都在我的最后工作正常。你是否看到任何特定的错误或其他行为? –

+0

它运作良好,唯一的问题是我必须在点击两次之前点击按钮。它适合你吗?你使用的是什么浏览器? –

+0

我试过IE11和Chrome。 –

回答

2

你的朋友是正确的,你需要了解页面Page Life循环更好。基本上在这种情况下,您需要了解OnLoad事件在之前发生的任何点击事件。你可以通过给OnLoad事件和点击处理程序添加一个断点来看到这一点。你会看到事件发生的顺序。

在这种情况下我会写诗的方法来设置页面,然后在每个调用这个上点击事件

private void setUpPage() 
{ 
    if (Session["User"] != null) 
    { 
     RegisterRelated.Visible = false; 
     LoginRelated.Visible = false; 
     InPage.Visible = true; 
     WelcomeTag.Text = "Welcome, " + ((User)Session["User"]).Username + "."; 
    } 
    else 
    { 
     RegisterRelated.Visible = true; 
     LoginRelated.Visible = true; 
     WelcomeTag.Text = String.Empty; 
     InPage.Visible = false; 
    } 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Call if not in response to button click 
    if(!IsPostBack) 
    { 
     setUpPage(); 
    } 
} 

protected void Register_Click(object sender, EventArgs e) 
{ 
    Label Reply = new Label(); 
    if (Session["User"] == null) 
    { 
     Result myRegResult = Result.IN_PROG; 
     User myAddedUser = new User(UserTB.Text, PasswordTB.Text, EmailTB.Text); 
     DbManager.OpenDbConnection(); 
     myRegResult = DbManager.Register(myAddedUser); //Connection with the database. 
     Reply.Text = resultToString(myRegResult); 
     Reply.ForeColor = resultColor(myRegResult); 
    } 
    else 
    { 
     Reply.Text = "You must log out before you register."; 
     Reply.ForeColor = resultColor(Result.EXEC_ERROR); 
    } 
    Answer.Controls.Add((Control)Reply); 
    //Reset_Fields(); 

    //Reset the fields as required AFTER you have done what you need with the database 
    setUpPage(); 
} 


protected void Login_Click(object sender, EventArgs e) 
{ 
    Label Reply = new Label(); 
    LoginProc Status = LoginProc.IN_PROG; 
    DbManager.OpenDbConnection(); 
    Status = DbManager.Login(LoginUserTB.Text, LoginPassTB.Text); //Connection with the database 
    Reply.Text = ProcToString(Status); 
    Reply.ForeColor = ProcToColor(Status); 
    LogAnswer.Controls.Add(Reply); 
    if (Status == LoginProc.FINE) 
     Session["User"] = new User(LoginUserTB.Text, LoginPassTB.Text, null); 
    //Reset_Fields(); 
    //Reset the fields as required AFTER you have done what you need with the database 
    setUpPage(); 
} 



protected void logout_Click(object sender, EventArgs e) 
{ 
    Session["User"] = null; 
    //Reset the fields as required AFTER you have done what you need with the database 
    setUpPage(); 
} 
+0

它工作正常!详细的解决方案。非常感谢你。如果我可以的话,我会赞扬你。 –

0

我认为验证器存在问题。请根据您的要求设置按钮的验证属性。

+0

CausesValidation当前为“true”。你认为改变它会有帮助吗? –

+0

对于所有的按钮,这是真的吗? – Amit

0

尝试Page_BlockSubmit = false;每前return false;

+0

我没有任何按钮点击返回false,我想?我用javascript代码试了一下,并没有太大的改变。 –

1

当您单击登录或注册按钮,页面加载事件工作首先,和按钮单击事件的作品。

我可以看到,您在页面加载事件中设置为页面显示,并在按钮单击事件中设置为会话值。因此,首先点击,首先触发页面加载事件,但没有会话值。页面加载事件通过按钮单击事件完成并继续,因此会话值现在不为空(如果输入的用户信息有效)。 这是第二次点击页面时的原因。

解决方案:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (this.IsPostBack) //just write this 
       return; 

     if (Session["User"] != null) 
     { 
      RegisterRelated.Visible = false; 
      LoginRelated.Visible = false; 
      InPage.Visible = true; 
      WelcomeTag.Text = "Welcome, " + ((User)Session["User"]).Username + "."; 
     } 
     else 
     { 
      RegisterRelated.Visible = true; 
      LoginRelated.Visible = true; 
      WelcomeTag.Text = String.Empty; 
      InPage.Visible = false; 
     } 
    } 

注:我收到了你的代码,并试图。

也看到这一点:http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx

+0

我不喜欢在签名中包含'void'的方法/函数中放置返回语句。此外,你并没有阻止这个帖子回来,只是意识到帖子后面。 –

+0

非常感谢您的时间,UGUR,您的解决方案是正确的(: –

+0

)欢迎您。请不要忘记接受我为其他程序员解答同样问题的程序员的回答;) –