我是 ASP.NET 和 C# 的新手。我试图从文件夹中检索所有图像并将其显示在页面上,但它只选择了一个图像。
我的 ASP.NET 代码:
<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 表中的图像。
附加: 有没有办法从路径存储在 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;?>
最佳答案
在后面的代码中编写 Collection()
方法来检索图像作为 List
的 Strings
像这样(另外最好使用 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());
}
}
}
}
}
然后你可以像这样使用
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>
或者你可以这样做:
<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>
关于c# - 使用 ASP.NET 选择所有图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33597934/