2

我需要知道如何从下拉列表中显示数据。例如:

下拉列表

选择图片
汽车

钓鱼

用户看到的第一件事是选择图像下拉菜单。用户将看到从选择图像下拉列表中显示的一些随机图片。

如果用户按下列表中的汽车图片,则会显示新图片,以此类推。

每张图片都会显示在 html 表格中,类似于我绘制的图片(下图)。假设每个列表有三张图片,那么这三张图片中的每一张都会显示在表格中(如下所示)。

示例截图

这是我到目前为止编写的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections;


namespace Prototype
{
    public partial class HomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillddlPictuer();
            }
        }

        public void FillddlPictuer()
        {
            string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM pictuer", conn);

                try
                {
                    conn.Open();
                    SqlDataReader readeer = cmd.ExecuteReader();

                    ListItem newItem = new ListItem();
                    newItem.Text = "Select Image";
                    newItem.Value = "0";
                    ddlMovie.Items.Add(newItem);

                    while (readeer.Read())
                    {
                        newItem = new ListItem();
                        newItem.Text = readeer["name"].ToString();
                        newItem.Value = readeer["id"].ToString();
                        ddlMovie.Items.Add(newItem);
                    }
                    StringBuilder sb = new StringBuilder();

                }

                catch
                { 
                    //Handel any error
                    conn.Close();
                }



            } //Close the first using  
        }
    }
}

主页代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="HomePage.aspx.cs" Inherits="Prototype.HomePage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMainSection" runat="server">
    <div id="ImageGalleryBorder"></div>
    <div id="ChampionBorder"></div>
    <div id="OtherStuffBorder">  
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderMainAside" runat="server">
    <h1>New videos</h1>
    <asp:DropDownList ID="ddlMovie" runat="server"
        CssClass="DropDownListAside">
    </asp:DropDownList>
    <asp:Label ID="lblOutput" runat="server" Text="Label" Visible="False"></asp:Label>
    <br />
</asp:Content>
4

1 回答 1

3
  1. 更改 SQL 中的图片表以包含路径列,这将用于存储服务器上图像的路径:
  2. 将 Pictuer 表重命名为Picture

在此处输入图像描述

ASPX:

<asp:ScriptManager ID="sm" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlMovie" runat="server" AutoPostBack="true" OnSelectedIndexChanged="MovieChanged" />
        <asp:PlaceHolder ID="pictures" runat="server" />
        <span id="error" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetPictures();
        AddPicture(ddlMovie.SelectedItem.Value);
        ReDisplayPictures();
    }
}

private void ReDisplayPictures()
{
    List<string> imagePaths = ViewState["Images"] as List<string>;
    if (imagePaths != null)
    {
        foreach (string path in imagePaths)
        {
            var image = new Image{Width = 100,Height = 100,ImageUrl = path};
            pictures.Controls.Add(image);
        }
    }
}

private void AddPicture(string imageUrl)
{
    List<string> imagePaths = ViewState["Images"] as List<string>;
    if (imagePaths == null)
        imagePaths = new List<string>();

    imagePaths.Add(imageUrl);
    ViewState["Images"] = imagePaths;
}

protected void MovieChanged(object sender, EventArgs e)
{
    AddPicture(ddlMovie.SelectedItem.Value);
    ReDisplayPictures();
}

private void GetPictures()
{
    try
    {
        string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (var conn = new SqlConnection(cs))
        {
            using (var command = new SqlCommand("SELECT * FROM Picture", conn))
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string name = reader["name"].ToString();
                    string path = reader["path"].ToString();
                    var item = new ListItem { Text = name, Value = path };
                    ddlMovie.Items.Add(item);
                }
            }
            conn.Close();
        }
    }
    catch (Exception eX)
    {
        error.InnerHtml = String.Format("An error occured, description - {0}",
            eX.Message);
    }
}

}

我为您创建了一个示例项目,您可以从 Google 驱动器在此处下载(只需单击文件->下载)

于 2013-04-11T13:53:57.193 回答