2011-11-08 31 views
0

我的註冊按鈕事件是當用戶註冊成功並將他重定向到登錄頁面時,如何顯示消息框?

protected void signup_Click(object sender, EventArgs e) 
    { 
     string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString; 
     SqlConnection conn = new SqlConnection(con); 
     conn.Open(); 
     if (selectques.SelectedItem.Text == "Write your own question?") 
     { 
      SqlCommand cmd = new SqlCommand("insert into registration values('" + username.Text + "','" + passwrd.Text + "','" + emailadd.Text + "','" + alterquestion.Text + "','" + securityanswer.Text + "')", conn); 
      cmd.ExecuteNonQuery(); 
      Response.Redirect("Login.aspx"); 
      try { 
       ClientScript.RegisterStartupScript(Page.GetType(), "Message", "alert('Successful Registered');window.location='login.aspx';", true); 
      } 
      catch(Exception ex) 
      { 
      } 
     } 
     else 
     { 
      SqlCommand cmd = new SqlCommand("insert into registration values('" + username.Text + "','" + passwrd.Text + "','" + emailadd.Text + "','" + selectques.Text + "','" + securityanswer.Text + "')", conn); 
      cmd.ExecuteNonQuery(); 
      Response.Redirect("login.aspx"); 
      try 
      { 
       ClientScript.RegisterStartupScript(Page.GetType(), "Message", "alert('Successful Registered');window.location='login.aspx';", true); 
      } 
      catch (Exception ex) 
      { 
      } 
     } 
    } 

成功註冊我怎麼能顯示出Successful Registered消息在登錄頁面或在同一頁上後。我想通過彈出窗口或消息框顯示消息。

+0

刪除「Response.Redirect(」Login.aspx「);」並檢查... – Nalaka526

+0

下面的答案提出了一個很好的觀點,你的SQL代碼在這裏是非常危險的。除了明顯的SQL注入問題,您不應該直接在ASP.NET頁面內執行SQL。爲了可維護性,您應該從Web層代碼中分離出您的數據訪問代碼。 – SoWeLie

+0

@ Nalaka526非常感謝你解決了我的問題。我告訴你我在執行我需要的代碼之前將它重定向到另一頁上。謝謝發佈它作爲答案我會接受它。 – avirk

回答

1

刪除Response.Redirect("Login.aspx");並檢查。

+0

@avirk也考慮其他答案和評論中的建議和建議... – Nalaka526

0

它看起來像你點擊註冊按鈕,這意味着你需要使一點JavaScript來顯示彈出,然後重定向,而不是做重定向服務器端時提交表單。

您的其他選擇是通過調用AJAX的註冊動作,如果註冊成功,再次顯示該警報的客戶端,並重定向客戶端。

+0

我不知道AJAX可以幫助我嗎? – avirk

+0

這是一個相當廣泛的事情,我可以指出你的方向的一些信息,雖然: http://en.wikipedia.org/wiki/Ajax_%28programming%29 我建議使用JQuery的簡單的應用程序:http://api.jquery.com/category/ajax/。 – SoWeLie

3

一個小的UI設計祕訣 - 人討厭的彈出窗口。當然,你提出的想法需要JavaScript--有些人已經禁用了。 無障礙解決方案。最簡單的方法是通過GET傳遞參數(你也可以使用會話)來login.aspx告訴它達到或接近頁面說註冊成功,或許與CSS樣式的頂部呼應的附加消息,以使它看起來像一個窗口。

但請,不alert S和沒有彈出。這是非常糟糕的設計。

2

我敢肯定,你應該對SQL注入閱讀了太多,因爲你在這裏做是非常危險的。

SqlCommand cmd = new SqlCommand(
    "insert into registration values('" + 
    username.Text + "','" + passwrd.Text + "','" + 
    emailadd.Text + "','" + alterquestion.Text + "','" + 
    securityanswer.Text + "')", conn); 
0

移動Response.Redirect("Login.aspx");

相關問題