如何在C#中使用switch case

时间:2015-03-25 18:19:22

标签: c# asp.net

我在这里尝试做的只是向用户显示一条消息:

  

您已成功创建帐户

然后将它们重定向到登录页面,如果他们输入正确的信息。

但是,我的问题是,如果用户输入无效的用户名或无效的临时密码,它会显示一个空白的Windows警告框,然后将页面重定向到登录页面。

我只是想显示错误消息,如果他们输错了信息而不是重定向页面,如果他们输入了正确的信息,那么它将显示成功消息并将页面重定向到登录。

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);

3 个答案:

答案 0 :(得分:2)

您需要确保仅在成功创建帐户的情况下注册脚本。您始终注册脚本,因此始终显示警报框。

为什么不将脚本及其注册移动到switch case的默认块中。这应该可以解决你的问题。

           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 :(得分:1)

我猜你必须在案例-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;
}

答案 2 :(得分:1)

试试这个

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);
相关问题