使用ASP.NET选择所有图像

13 c# sql asp.net

我是ASP.NET和C#的新手.我正在尝试从文件夹中检索所有图像并在页面上显示它,但它只选择一个图像.

我的ASP.NET代码:

<form id="form1" runat="server" class="col-lg-5">            
    <asp:Image ID="Image" runat="server" />
</form>
Run Code Online (Sandbox Code Playgroud)

我的C#代码:

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.Data;
using System.Configuration;

namespace Blog
{
    public partial class index : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["blogconnection"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            string allimage;
            string qry="select * from images";
            SqlCommand cmd = new SqlCommand(qry, con);
            SqlDataReader dr =cmd.ExecuteReader();

            if (dr.HasRows)
            {
                while(dr.Read())
                {
                   if (!string.IsNullOrEmpty(Convert.ToString(dr["Image_Path"])))
                   {                                             
                        Image.ImageUrl = Convert.ToString(dr["Image_Path"]);                                         
                   }
                }
           }
           con.Close();
       }               
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要的是: 我想选择所有在sql表中存储路径的图像.

附加:有没有办法从文件夹中选择路径存储在sql中的视频,意思是选择不同文件夹中的视频和图像,并按指定日期或最新上传等显示在同一页面上.

任何帮助将不胜感激.

编辑#1

在PHP中我使用下面的代码来获取所有图像并显示它,是否有任何东西相当于ASP.NET中的下面的代码?

PHP代码

<?php
include 'conn.php';
$smt=$conn->prepare('SELECT * FROM post');
$smt->execute();

?>
<?php include 'header.php';

?>
<div class="">
<?php
if(isset($_SESSION['user']))
{
include 'nav.php';
}
else
{
include 'nav-simple.php';
}
?>
<?php include 'slider.php';?>

<?php include 'right_sidebar.php';?>

    <div class="col-md-1 top_space"></div>
<div class="container col-md-8 main-container-top">


    <br/>

<div class="">
    <?php while ($gdata = $smt->fetch(PDO::FETCH_OBJ)): ?>
        <a href="#" class="col-md-4"><img src="posts/<?php echo $gdata->Post_Path; ?>" alt="image" class="post-image"/></a>
        <div class="media-body col-md-8 post pull-left">
            <div class="post-overview">
                <ul>
                    <li class="post-category"><?php echo $gdata->Category; ?></li>
                    <li class="post-timestemp">Post on <?php echo $gdata->Post_Date; ?></li>
                </ul>


                <a href="post-description.php?id=<?php echo $gdata->Id ?>"><h4
                        class="media-heading h4"><?php echo $gdata->Title; ?></h4></a>

                <p class="post-text"><?php echo $gdata->Post; ?></p><br/>
            </div>


        </div>
<div class="post-image-space"></div>
    <?php endwhile;?>
Run Code Online (Sandbox Code Playgroud)

Sal*_*ari 8

在后面的代码编写Collection()方法来检索图像作为一个ListStrings这样的(而且最好是用Using语句):

protected IEnumerable<string> Collection()
{
     string address = ConfigurationManager.ConnectionStrings["blogconnection"].ToString();
     using (SqlConnection con = new SqlConnection(address))
     {
         con.Open();
         string qry = "select * from images";
         SqlCommand cmd = new SqlCommand(qry, con);
         using (SqlDataReader dr = cmd.ExecuteReader())
         {
             if (!dr.HasRows) return allimage;
             while (dr.Read())
             {
                 if (!string.IsNullOrEmpty(Convert.ToString(dr["Image_Path"])))
                 {
                     yield return (dr["Image_Path"].ToString());
                 }
              }
          }
      }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样使用asp:Repeater:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="imgCats">
      <ItemTemplate>
         <div>
            <img src='<%# Container.DataItem.ToString() %>' alt="" />
         </div>                  
      </ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="imgCats" runat="server" SelectMethod="Collection" 
          TypeName="WebApplication1.WebForm8">
</asp:ObjectDataSource>
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:

<form id="form1" runat="server" class="col-lg-5">
  <div>
    <ul>
        <% var drc = Collection();
           foreach (var item in drc)
           { %>
            <li>
                <img src="<%: item %>"/>
            </li>
        <% } %>
    </ul>
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)


Geo*_*eMR 3

您可以动态地将新的 asp:image 对象添加到 form1。

Image img = new Image();
img.ImageUrl = dr["Image_Path"].ToString();
img.AlternateText = "Test image";
form1.Controls.Add(img);
Run Code Online (Sandbox Code Playgroud)