How to use switch case in C#

What I am trying to do here is simply to show the user a message that says:

you have successfully created your account

and then redirect them to the login page, if they enter the correct info.

However, my issue is that if the user puts an invalid username or invalid temp password, it shows a blank windows alert box and then it redirects the page to the login page.

I just want to show the error message if they put wrong info and not redirect the page, and if they put the correct info, then it will show the success message and redirect the page to the login.

string message = string.Empty;

switch (userId)
{
    case -1:
        errLbl.Text = "The current temporary password you entered is invalid.nPlease re-enter your current temporary password.";
        break;
    case -2:
        errLbl.Text = "Username already exists. Please choose a different username";
        break;
    default:
        message = "You have successfully created your account.  You will now be redirected to the Login page.  Thanks.";
        break;
}

string url = "Login";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);

You need to ensure that you are registering the script only in a case where the account is successfully created. You are always registering the script, due to which the alert box is always shown.

Why don't you move the script and its registration into the default block of the switch case. That should solve your problem.

           string message = string.Empty;
           switch (userId)
           {
               case -1:
                   errLbl.Text = "The current temporary password you entered is invalid.nPlease re-enter your current temporary password.";
                   break;
               case -2:
                   errLbl.Text = "Username already exists. Please choose a different username";
                   break;
               default:
                   message = "You have successfully created your account.  You will now be redirected to the Login page.  Thanks.";
           string url = "Login";
           string script = "window.onload = function(){ alert('";
           script += message;
           script += "');";
           script += "window.location = '";
           script += url;
           script += "'; }";
           ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);

                   break;
           }

我猜你必须在-1和-2的情况下给消息分配一些字符串,否则你的消息将保持空白。

switch (userId)
{
    case -1:
      message = "The current temporary password you entered is invalid.nPlease re-enter your current temporary password.";
      break;
    case -2:
      message = "Username already exists. Please choose a different username";
      break;
    default:
      message = "You have successfully created your account.  You will now be redirected to the Login page.  Thanks.";
      break;
}

像这样尝试

string url = "Login";
string successScript="";
string message = string.Empty;
switch (userId)
{
    case -1:
        message = "The current temporary password you entered is invalid.nPlease re-enter your current temporary password.";
        errLbl.Text = "The current temporary password you entered is invalid.nPlease re-enter your current temporary password.";
        break;
    case -2:
        message = "Username already exists. Please choose a different username";
        errLbl.Text = "Username already exists. Please choose a different username";
        break;
    default:
        message = "You have successfully created your account.  You will now be redirected to the Login page.  Thanks.";
        successScript += "window.location = '";
        successScript += url;
        successScript += "'; }";                           
        break;
}
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += successScript;

ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
链接地址: http://www.djcxy.com/p/71542.html

上一篇: 如何在延迟后重定向到其他页面

下一篇: 如何在C#中使用开关箱