【问题标题】:Displaying different image using the drop down list (ASP.net MVC)使用下拉列表显示不同的图像(ASP.net MVC)
【发布时间】: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>&nbsp;<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>&nbsp;</td>
                </tr>
                <tr>
                    <td>Username: </td>
                    <td>&nbsp;<asp:Textbox type="text" ID="txtUsername" placeholder="Enter Username" runat="server"></asp:Textbox></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Password: </td>
                    <td>&nbsp;<asp:TextBox type="password" ID="txtPassword" placeholder="Enter password" runat="server" TextMode="Password"></asp:TextBox></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;<asp:Button class="mybtn" runat="server" Text="Login" OnClick="BtnLogin_Click"/></td>
                    <td>&nbsp;</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


【解决方案1】:

您可以使用 javascript/jquery 在客户端执行此操作。为下拉的 OnChange 事件挂钩一个 javascript 函数。像这样https://codepen.io/joshwentz/pen/jEKVoz

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>

<select onchange="$('#imageToSwap').attr('src', this.options[this.selectedIndex].value);">
    <option value="http://cdns2.freepik.com/free-photo/facebook-logo_318-49940.jpg" selected>Facebook</option>
    <option value="http://cdns2.freepik.com/free-photo/twitter-logo_318-40459.jpg">Twitter</option>
    <option value="http://cdns2.freepik.com/free-photo/instagram-social-network-logo-of-photo-camera_318-64651.jpg">Instagram</option>
</select>

<br><br>

<img id="imageToSwap" class="profile" src="http://cdns2.freepik.com/free-photo/facebook-logo_318-49940.jpg">

【讨论】:

    【解决方案2】:

    我可以通过在我的 Login.aspx.cs 中创建一个事件来更改照片

    protected void UserType_SelectedIndexChanged(object sender, EventArgs e)
            {
                ImageDef.ImageUrl = "Images/student.png";
                {
                    if (userType.SelectedValue == "Student")
                    {
                        ImageDef.ImageUrl = "Images/student.png";
                    }
                    else if (userType.SelectedValue == "Teacher")
                    {
                        ImageDef.ImageUrl = "Images/teacher.png";
                    }
                    else if (userType.SelectedValue == "Counselor")
                    {
                        ImageDef.ImageUrl = "Images/counselor.png";
                    }
                    else if (userType.SelectedValue == "Parent")
                    {
                        ImageDef.ImageUrl = "Images/parent.png";
                    }
                    else if (userType.SelectedValue == "Principal")
                    {
                        ImageDef.ImageUrl = "Images/principal.png";
                    }
                    else if (userType.SelectedValue == "Admin")
                    {
                        ImageDef.ImageUrl = "Images/admin.png";
                    }
                    else
                    {
                        ImageDef.ImageUrl = "Images/student.png";
                    }
                }
            }
    

    在我的 login.aspx 中,我为学生创建了一个默认图像,因为它是我下拉列表中的第一个列表。我添加了这个 AutoPostBack="True" OnSelectedIndexChanged="UserType_SelectedIndexChanged" 以确保在选择某个列表时,它会更改事件中的照片。

    <div class="imgcontainer" style="text-align:center">
                <asp:Image ID="ImageDef" ImageUrl="Images/student.png" runat="server" class="avatar" />
            </div>
    
            <table class="container">
                    <tr>
                        <td>Select User Type: </td>
                        <td>&nbsp;<asp:Label ID="Button1" runat="server"></asp:Label>
                            <asp:DropDownList ID="userType" runat="server" RepeatDirection="Horizontal" Height="20px" Width="155px" AutoPostBack="True" OnSelectedIndexChanged="UserType_SelectedIndexChanged">
                            <asp:ListItem Value="Student">Student</asp:ListItem>
                            <asp:ListItem Value="Teacher">Teacher</asp:ListItem>
                            <asp:ListItem Value="Counselor">Counselor</asp:ListItem>
                            <asp:ListItem Value="Parent">Parent</asp:ListItem>
                            <asp:ListItem Value="Principal">Principal</asp:ListItem>
                            <asp:ListItem Value="Admin">Admin</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>&nbsp;</td>
                    </tr>
    

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2021-08-21
      • 2016-08-03
      • 2016-09-23
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多