日期:2014-05-17 浏览次数:20420 次
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
FileModel a = new FileModel() { FileId = 1, Name = "Test1", Size = "10kb", CreatedDate = DateTime.Now };
FileModel b = new FileModel() { FileId = 2, Name = "Test2", Size = "20kb", CreatedDate = DateTime.Now };
FileModel c = new FileModel() { FileId = 3, Name = "Test3", Size = "30kb", CreatedDate = DateTime.Now };
List<FileModel> lst = new List<FileModel>();
lst.Add(a);
lst.Add(b);
lst.Add(c);
this.Repearter1.DataSource = lst;
this.Repearter1.DataBind();
}
}
protected void Repearter1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
HiddenField hdFileID = (HiddenField)e.Item.FindControl("hdFileID");
if (hdFileID != null) {
FileModel file = (FileModel)e.Item.DataItem;
hdFileID.Value = file.FileId.ToString();
}
}
protected void btnDownload_Click(object sender, EventArgs e) {
HiddenField hdFileId = (HiddenField)(((Button)sender).Parent).FindControl("hdFileID");
if (hdFileId != null) {
// 根据FileId可以获取文件的相关信息,比如路径等,然后下面实现下载
}
}
public class FileModel
{
public int FileId { get; set; }
public string Name { get; set; }
public string Size { get; set; }
public DateTime CreatedDate { get; set; }
}
<asp:Repeater ID="Repearter1" runat="server" onitemdatabound="Repearter1_ItemDataBound">