如何在 Asp.Net 中的 DataList 中显示逗号分隔的图像?
How to display comma separated images in DataList in Asp.Net?
我正在开发 asp.net c# 应用程序,因为我有一个场景,用于在 DataList 中显示逗号分隔的图像,如下图名称
在 image_url 字段中
tmp_IMG_20170522_0837451077517149.jpg,
tmp_IMG_20170522_083635-993558832.jpg,
tmp_IMG_20170522_083327-596532566.jpg,tmp_IMG_20170522_0839271082156791.jpg,
tmp_IMG_20170522_092501-744906076.jpg,
tmp_IMG_20170522_084036-2041074160.jpg,
tmp_IMG_20170522_091330-34718281.jpg,
tmp_IMG_20170522_092627-1388224780.jpg,
tmp_IMG_20170522_082648-859515701.jpg,tmp_IMG_20170522_083853-597136928.jpg
假设如果该字段只有一个图像名称意味着它将显示在DataList中,
但我不知道如何在 DataList 中显示逗号分隔的图像,
请任何需要帮助的人。
Asp.Net :
<asp:DataList ID="DLImages" RepeatColumns="2" runat="server">
<ItemTemplate>
<div id='gallery'>
<a href='<%# Eval("image_url","../Uploads/App_Images/{0}")%>'>
<img src='<%# Eval("image_url","../Uploads/App_Images/{0}") %>' width="200" height="200" />
</a>
</div>
</ItemTemplate>
</asp:DataList>
C#:
private void FillDataList()
{
try
{
bolImages.ID=Convert.ToInt32(Request.QueryString["ID"].ToString());
DataSet dsImages = new DataSet();
dsImages = bolImages.SelectImagesByID();
if (dsImages.Tables[0].Rows.Count > 0)
{
DLImages.DataSource = dsImages;
DLImages.DataBind();
}
else
{
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
查询:
public DataSet SelectImagesByID()
{
try
{
Squery = @"select image_url from BMTEST..Images WHERE ID='" + ID + "';
return dal.DBRead(Squery);
}
catch (Exception ex)
{
throw ex;
}
}
示例:
如果image_url如下显示一张图片:
tmp_IMG_20170522_0837451077517149.jpg
但是如果是多个逗号分隔的图像如何显示。
tmp_IMG_20170522_083327-596532566.jpg,tmp_IMG_20170522_0839271082156791.jpg
你能试试这样吗,让我知道结果如何:
<div id='gallery'>
<a href='<%# string.Format("../Uploads/App_Images/{0}", Eval("image_url"))%>'>
<img src='<%# string.Format("../Uploads/App_Images/{0}", Eval("image_url"))%>' width="200" height="200" />
</a>
</div>
根据评论,很明显该行包含逗号分隔值,您需要将这些值作为数据源而不是行,因此您需要做的是像下面这样更改数据源:
if (dsImages.Tables[0].Rows.Count > 0)
{
int rowNumber = 0;
DLImages.DataSource = dsImages.Tables[0][rowNumber]["image_url"].Split(',').ToList();
DLImages.DataBind();
}
我认为您需要在将图像路径加载到数据集之前拆分以逗号分隔的图像路径
DLImages.DataSource = dsImages;
这里的 dsImages 应该是
dsImages.Split[','];
试试这个:
dsImages = bolImages.SelectImagesByID();
if (dsImages.Tables[0].Rows.Count > 0)
{
string[] values = dsImages.Split(',');
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}
var newdsImages = values.ToList();
DLImages.DataSource = newdsImages ;
DLImages.DataBind();
}
if (dsImages.Tables[0].Rows.Count > 0)
{
var filelists = dsImages.Tables[0].Rows[0]["image_url"].ToString().Split(',').ToList();
List<string[]> imagelist = new List<string[]>();
List<string[]> videolist = new List<string[]>();
if (filelists.Count >= 0)
{
for (int i = 0; i < filelists.Count; i++)
{
string FileName = filelists[i].ToString();
string Extension = Path.GetExtension(FileName);
if (Extension == ".jpg" || Extension == ".JPG" || Extension == ".jpeg" || Extension == ".JPEG" ||
Extension == ".gif" || Extension == ".GIF" || Extension == ".png" || Extension == ".PNG" ||
Extension == ".tiff" || Extension == ".TIFF" || Extension == ".bmp" || Extension == ".BMP")
{
imagelist.Add(new string[] { filelists[i].ToString() });
}
else
{
videolist.Add(new string[] { filelists[i].ToString() });
}
}
DataTable dtImages = ConvertListToDataTable(imagelist);
DLImages.DataSource = dtImages;
DLImages.DataBind();
DataTable dtVideo = ConvertListToDataTable(videolist);
DLVideos.DataSource = dtVideo;
DLVideos.DataBind();
}
}
else
{
}
static DataTable ConvertListToDataTable(List<string[]> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var array in list)
{
table.Rows.Add(array);
}
return table;
}
我正在开发 asp.net c# 应用程序,因为我有一个场景,用于在 DataList 中显示逗号分隔的图像,如下图名称 在 image_url 字段中
tmp_IMG_20170522_0837451077517149.jpg, tmp_IMG_20170522_083635-993558832.jpg, tmp_IMG_20170522_083327-596532566.jpg,tmp_IMG_20170522_0839271082156791.jpg, tmp_IMG_20170522_092501-744906076.jpg, tmp_IMG_20170522_084036-2041074160.jpg, tmp_IMG_20170522_091330-34718281.jpg, tmp_IMG_20170522_092627-1388224780.jpg, tmp_IMG_20170522_082648-859515701.jpg,tmp_IMG_20170522_083853-597136928.jpg
假设如果该字段只有一个图像名称意味着它将显示在DataList中,
但我不知道如何在 DataList 中显示逗号分隔的图像, 请任何需要帮助的人。
Asp.Net :
<asp:DataList ID="DLImages" RepeatColumns="2" runat="server">
<ItemTemplate>
<div id='gallery'>
<a href='<%# Eval("image_url","../Uploads/App_Images/{0}")%>'>
<img src='<%# Eval("image_url","../Uploads/App_Images/{0}") %>' width="200" height="200" />
</a>
</div>
</ItemTemplate>
</asp:DataList>
C#:
private void FillDataList()
{
try
{
bolImages.ID=Convert.ToInt32(Request.QueryString["ID"].ToString());
DataSet dsImages = new DataSet();
dsImages = bolImages.SelectImagesByID();
if (dsImages.Tables[0].Rows.Count > 0)
{
DLImages.DataSource = dsImages;
DLImages.DataBind();
}
else
{
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
查询:
public DataSet SelectImagesByID()
{
try
{
Squery = @"select image_url from BMTEST..Images WHERE ID='" + ID + "';
return dal.DBRead(Squery);
}
catch (Exception ex)
{
throw ex;
}
}
示例:
如果image_url如下显示一张图片:
tmp_IMG_20170522_0837451077517149.jpg
但是如果是多个逗号分隔的图像如何显示。
tmp_IMG_20170522_083327-596532566.jpg,tmp_IMG_20170522_0839271082156791.jpg
你能试试这样吗,让我知道结果如何:
<div id='gallery'>
<a href='<%# string.Format("../Uploads/App_Images/{0}", Eval("image_url"))%>'>
<img src='<%# string.Format("../Uploads/App_Images/{0}", Eval("image_url"))%>' width="200" height="200" />
</a>
</div>
根据评论,很明显该行包含逗号分隔值,您需要将这些值作为数据源而不是行,因此您需要做的是像下面这样更改数据源:
if (dsImages.Tables[0].Rows.Count > 0)
{
int rowNumber = 0;
DLImages.DataSource = dsImages.Tables[0][rowNumber]["image_url"].Split(',').ToList();
DLImages.DataBind();
}
我认为您需要在将图像路径加载到数据集之前拆分以逗号分隔的图像路径
DLImages.DataSource = dsImages;
这里的 dsImages 应该是
dsImages.Split[','];
试试这个:
dsImages = bolImages.SelectImagesByID();
if (dsImages.Tables[0].Rows.Count > 0)
{
string[] values = dsImages.Split(',');
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}
var newdsImages = values.ToList();
DLImages.DataSource = newdsImages ;
DLImages.DataBind();
}
if (dsImages.Tables[0].Rows.Count > 0)
{
var filelists = dsImages.Tables[0].Rows[0]["image_url"].ToString().Split(',').ToList();
List<string[]> imagelist = new List<string[]>();
List<string[]> videolist = new List<string[]>();
if (filelists.Count >= 0)
{
for (int i = 0; i < filelists.Count; i++)
{
string FileName = filelists[i].ToString();
string Extension = Path.GetExtension(FileName);
if (Extension == ".jpg" || Extension == ".JPG" || Extension == ".jpeg" || Extension == ".JPEG" ||
Extension == ".gif" || Extension == ".GIF" || Extension == ".png" || Extension == ".PNG" ||
Extension == ".tiff" || Extension == ".TIFF" || Extension == ".bmp" || Extension == ".BMP")
{
imagelist.Add(new string[] { filelists[i].ToString() });
}
else
{
videolist.Add(new string[] { filelists[i].ToString() });
}
}
DataTable dtImages = ConvertListToDataTable(imagelist);
DLImages.DataSource = dtImages;
DLImages.DataBind();
DataTable dtVideo = ConvertListToDataTable(videolist);
DLVideos.DataSource = dtVideo;
DLVideos.DataBind();
}
}
else
{
}
static DataTable ConvertListToDataTable(List<string[]> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var array in list)
{
table.Rows.Add(array);
}
return table;
}