idx
|
title
|
imgurl
|
<%#Eval("idx") %>
|
<%#Eval("title") %>
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
public partial class ListControls_DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataList1.DataSource = GetData();
this.DataList1.DataBind();
}
}
///
/// test 데이타.
///
///
private DataTable GetData()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("idx");
dt.Columns.Add("title");
dt.Columns.Add("regdate");
dt.Columns.Add("imgurl");
for (int i = 0; i < 11; i++)
{
dr = dt.NewRow();
dr["idx"] = i.ToString();
dr["title"] = String.Format("{0} 번째 제목", i.ToString());
dr["regdate"] = DateTime.Now.ToString("yyyy-MM-dd");
dr["imgurl"] = String.Format("{0}.gif", i.ToString());//파일명
dt.Rows.Add(dr);
dt.AcceptChanges();
}
return dt;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType.Equals(ListItemType.AlternatingItem) || e.Item.ItemType.Equals(ListItemType.Item))
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Image img = (Image)e.Item.FindControl("img");
// 파일의 물리적인 경로
string strFilePath = Server.MapPath(String.Format("~/ListControls/Files/{0}", drv["imgurl"].ToString()));
FileInfo file = new FileInfo(strFilePath);
// 파일존재여부 확인후 image 컴포넌트 숨김처리
if (file.Exists)
{
img.ImageUrl = String.Format("~/ListControls/Files/{0}", drv["imgurl"].ToString());
}
else
{
img.Visible = false;
}
}
}
}