在登录页面中,两个按钮用于授权用户,一个按钮用于Lecturer,另一个按钮用于学生,如何使用单个按钮执行讲师和学生使用if else语句...此项目使用C#ASP.NET
学生
protected void log_Click(object sender, EventArgs e)
{
Session["id"] = txtId1.Text;
FormsAuthentication.Initialize();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
SqlCommand com = conn.CreateCommand();
com.CommandText = "select [id], [name],[password], [rol] FROM [Student] WHERE [id] =@id AND [password]= @pass";
com.Parameters.Add("@id", txtId1.Text);
com.Parameters.Add("@pass", EncryptPassword(txtPass1.Text));
conn.Open();
SqlDataReader rd = com.ExecuteReader();
if (rd.Read())
{
var _id = rd["id"].ToString();
var _role = rd["rol"].ToString();
var _fname = rd["name"].ToString();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
_id,
DateTime.Now,
DateTime.Now.AddMonths(1),
true,
_role + "." + _fname,
FormsAuthentication.FormsCookiePath);
string hashed_ticket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashed_ticket);
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
// Response.Redirect("~/Admin/Profile.aspx");
if (_role == "student")
{
Response.Redirect("~/Student/Profile.aspx");
}
rd.Close();
conn.Close();
}
else
{
lb1.Text = "Invalid username or password.";
}
}
讲师
protected void btLogin_Click(object sender, EventArgs e)
{
Session["id"] = txtId.Text;
FormsAuthentication.Initialize();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
SqlCommand com = conn.CreateCommand();
// conn.Open();
com.CommandText = "select [id], [fname],[password], [role] FROM [Lecturer] WHERE [id] =@id AND [password]= @pass";
com.Parameters.Add("@id", txtId.Text);
com.Parameters.Add("@pass", EncryptPassword(txtPass.Text));
conn.Open();
SqlDataReader rd = com.ExecuteReader();
if (rd.Read())
{
var _id = rd["id"].ToString();
var _role = rd["role"].ToString();
var _fname = rd["fname"].ToString();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
_id,
DateTime.Now,
DateTime.Now.AddMonths(1),
true,
_role + "." + _fname,
FormsAuthentication.FormsCookiePath);
string hashed_ticket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashed_ticket);
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
// Response.Redirect("~/Admin/Profile.aspx");
if (_role == "Admin")
{
Response.Redirect("~/Admin/Profile.aspx");
}
else if (_role == "Lecturer")
{
Response.Redirect("~/Lecturer/CS.aspx");
}
rd.Close();
conn.Close();
}
else
{
lb.Text = "Invalid username or password.";
}
}
如何使用一个按钮执行学生和讲师登录按钮。
答案 0 :(得分:0)
理想情况下,您需要3个表,并且只允许用户输入用户名和密码。用户通过身份验证后,将根据用户的授权角色重定向到页面。
然而,这不是你原来的问题。
您需要RadioButtonList或DropDownList来选择人的类型。然后根据选择更改CommandText。