aspx Repeater 控件写个图片显示的页面 图片路径在一个字段中用;分割的 数量是动态的 从数据库获取
以下是一个可以显示多张图片的 aspx Repeater 控件页面示例,图片路径在一个字段中用分号分割,数量是动态从数据库获取的:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>图片显示页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptImages" runat="server">
<ItemTemplate>
<img src='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
在后台代码中,我们可以从数据库中获取图片路径,然后将其分号分割后绑定到 Repeater 控件中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 从数据库中获取图片路径
string imagePaths = GetImagePaths();
// 将图片路径分号分割后绑定到 Repeater 控件
rptImages.DataSource = imagePaths.Split(';');
rptImages.DataBind();
}
}
private string GetImagePaths()
{
// 从数据库中获取图片路径,这里假设图片路径存储在名为 "ImagePaths" 的字段中
// 并且数据表名为 "Images"
string connectionString = "your connection string";
string query = "SELECT ImagePaths FROM Images";
string imagePaths = "";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
imagePaths += reader["ImagePaths"].ToString() + ";";
}
reader.Close();
}
return imagePaths;
}
``
原文地址: https://www.cveoy.top/t/topic/g48A 著作权归作者所有。请勿转载和采集!