使用ASP.NET选择所有图像 [英] Select all images using ASP.NET

查看:74
本文介绍了使用ASP.NET选择所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to ASP.NET and C#. I am trying to retrieve all images from folder and show it on page, but it's only selecting one image.

我的ASP.NET代码:

My ASP.NET code:

<form id="form1" runat="server" class="col-lg-5">            
    <asp:Image ID="Image" runat="server" />
</form>

我的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();
       }               
    }
}

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

What I want: I want to select all image which path is store in sql table.

附加:有一种方法可以从文件夹中选择视频(路径存储在sql中),意味着从不同的文件夹中选择视频和图像,并按指定的日期或最新上传的内容在同一页面上显示等

Additional: is there a way to select videos from folder which path is store in sql, mean to select videos and images from different folder and show both on same page by date specified or by latest upload etc.

任何帮助将不胜感激.

编辑#1

在php中,我使用下面的代码来获取所有图像并将其显示出来,ASP.NET中是否有与下面的代码等效的东西?

In php i use the below code to get all images and show it, is there any thing equivalent to the below code in 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;?>

推荐答案

在后面的代码中,编写您的Collection()方法以像StringsList一样检索图像(使用 Using 语句):

In the code behind write your Collection() method to retrieve images as a List of Strings like this (Also it is better to use Using statements):

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());
                 }
              }
          }
      }
}

然后您可以像这样使用asp:Repeater:

Then either you can use asp:Repeater like this:

<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>

或者您可以这样做:

<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>

这篇关于使用ASP.NET选择所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆