2015-01-06 147 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

)歡迎您。請不要忘記接受我爲其他程序員解答同樣問題的程序員的回答;) –