如果,Else If Statement帮助 [英] If, Else If Statement help
本文介绍了如果,Else If Statement帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用If语句检查并查看三个级别吗?我正在做的是我有两个不同级别的登录工作。我想添加第三级登录。这就是我到目前为止。
Can I have the If statement to check and see for three levels? What I am doing is that I have it working for two different levels for login. I wanted to add a third level login. This is what I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
con.Open();
if (true)
{
SqlCommand level = new SqlCommand("select AccessLevel, Password from TableSecurity where EmailAddress = @EmailAddress AND Password = @Password", con);
level.Parameters.Add(new SqlParameter("EmailAddress", TextBoxEA.Text));
level.Parameters.Add(new SqlParameter("Password", TextBoxPW.Text));
SqlDataReader reader = level.ExecuteReader();
DataTable dt1 = new DataTable();
dt1.Load(reader);
foreach (DataRow dr1 in dt1.Rows)
{
int returnedLevel = Convert.ToInt32(dr1[0].ToString());
if (returnedLevel == 1)
{
Response.Redirect("CEOPage.aspx");
}
else if (returnedLevel == 2)
{
Response.Redirect("IALOPage.aspx");
}
}
}
con.Close();
}
}
如何在代码中添加3级?
How can I add level 3 to the code?
推荐答案
继续CHill60的回答:
Further to CHill60's answer:
string[] levelPages = new string[] { "CEOPage.aspx", "IALOPage.aspx", "SomeOtherPage.aspx", };
//...
Response.Redirect(levelPages[returnedLevel - 1]);
继续jkirkerx的建议...
Further to jkirkerx' suggestion ...
switch (returnedlevel)
{
case 1:
Response.Redirect("CEOPage.aspx");
break;
case 2:
Response.Redirect("IALOPage.aspx");
break;
case 3:
Response.Redirect("SomeOtherPage.aspx");
break;
default:
break;
}
您可以根据需要添加任意数量的其他级别,也可以在最后添加一个catch-all其他块:
You can add as many elseif levels as you like, and optionally at the end, you could add a catch-all else block:
if (returnedLevel == 1)
{
Response.Redirect("CEOPage.aspx");
}
else if (returnedLevel == 2)
{
Response.Redirect("IALOPage.aspx");
}
else if (returnedLevel == 3)
{
Response.Redirect("SomeOtherPage.aspx");
}
else
{
Response.Redirect("SomeDefaultPage.aspx");
}
正如jkirkerx在评论中所建议的,开关/ Case语句也可以。
And as jkirkerx suggested in the comments, a switch/Case statement would work as well.
这篇关于如果,Else If Statement帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文