【发布时间】:2018-02-27 09:30:09
【问题描述】:
我目前正在创建一个学生数据库,并且在从登录页面的下拉列表中选择不同选项时需要帮助来显示不同的图像。该列表包括不同用户类型的列表,例如学生、教师、家长、校长、管理员等。
我的 login.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace LoginQuery
{
public partial class Login : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=CHRIS\\SQLEXPRESS;Initial Catalog=FPSDD;Integrated Security=True";
con.Open();
}
protected void BtnLogin_Click(object sender, EventArgs e)
{
cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and PersonType='" + DropDownList3.SelectedValue + "'";
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(ds, "Person");
if (ds.Tables[0].Rows.Count > 0)
{
Response.Redirect(url: "http://localhost:56061/");
}
else
{
cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "'";
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(ds, "Person");
if (ds.Tables[0].Rows.Count > 0)
{
Label1.Text = "Invalid User Type. Please Try Again!";
}
else
{
Label1.Text = "Invalid User Type, Username or Password. Please Try Again!";
}
}
}
}
}
我的 login.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="LoginQuery.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
body {font-family: Verdana, Geneva, Tahoma, sans-serif;}
form {border: 3px solid #f1f1f1;}
h1 {color: #da2f3f;}
input.textbox[type=text], input.textbox[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
.mybtn {
background-color: #da2f3f;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.8;
}
.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}
img.avatar {
width: 15%;
border-radius: 50%;
}
.container {
padding: 16px;
margin: 0 auto;
}
</style>
</head>
<body>
<h1 style="text-align: center";>Login</h1>
<form runat="server">
<div class="imgcontainer" style="text-align:center">
<img src="Images/student.png" alt="Avatar" class="avatar" />
</div>
<table class="container">
<tr>
<td>Select User Type: </td>
<td> <asp:Label ID="Button1" runat="server"></asp:Label>
<asp:DropDownList ID="DropDownList3" runat="server" Height="20px" Width="155px">
<asp:ListItem>Student</asp:ListItem>
<asp:ListItem>Teacher</asp:ListItem>
<asp:ListItem>Counselor</asp:ListItem>
<asp:ListItem>Parent</asp:ListItem>
<asp:ListItem>Principal</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
</tr>
<tr>
<td>Username: </td>
<td> <asp:Textbox type="text" ID="txtUsername" placeholder="Enter Username" runat="server"></asp:Textbox></td>
<td> </td>
</tr>
<tr>
<td>Password: </td>
<td> <asp:TextBox type="password" ID="txtPassword" placeholder="Enter password" runat="server" TextMode="Password"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> <asp:Button class="mybtn" runat="server" Text="Login" OnClick="BtnLogin_Click"/></td>
<td> </td>
</tr>
</table>
<div style="margin-left: auto; margin-right: auto; text-align: center;"><asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label></div>
</form>
</body>
</html>
最终,我希望它看起来像下面的照片: This is the student view when the student in the list item is chosen.
This is the student view when the teacher in the list item is chosen.
【问题讨论】:
-
你应该处理 SelectedIndexChanged 事件...
-
我们确定这是一个 MVC 项目吗? .aspx 表示网络表单不是吗?
标签: c# asp.net visual-studio