[英]How to use switch case in C#
我在这里要做的只是向用户显示一条消息,内容是:
您已经成功创建了您的帐户
如果他们输入正确的信息,然后将它们重定向到登录页面。
但是,我的问题是,如果用户输入了无效的用户名或无效的临时密码,则会显示空白的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);
您需要确保仅在成功创建帐户的情况下才注册脚本。 您始终在注册脚本,因此始终会显示警报框。
为什么不将脚本及其注册移动到切换案例的默认块中。 那应该解决您的问题。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.