gpt4 book ai didi

c# - 使用 ASP.NET 选择所有图像

转载 作者:可可西里 更新时间:2023-11-01 08:10:00 25 4
gpt4 key购买 nike

我是 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() 方法来检索图像作为 StringsList,就像这样(也是最好使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com