如何在C#中使用开关箱
我在这里想要做的只是向用户显示一条消息:
您已成功创建您的帐户
然后将它们重定向到登录页面,如果他们输入正确的信息。
但是,我的问题是,如果用户放置了无效的用户名或无效的临时密码,它会显示一个空白的窗口警告框,然后将页面重定向到登录页面。
我只是想显示错误消息,如果他们把错误的信息,而不是重定向的页面,如果他们把正确的信息,然后它会显示成功的消息,并重定向到登录页面。
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);
您需要确保仅在成功创建帐户的情况下注册脚本。 您始终注册脚本,因此始终显示警告框。
你为什么不把脚本和它的注册移动到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和-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/71541.html
上一篇: How to use switch case in C#
下一篇: login page not redirect to reports page in asp.net mysql