事件处理
前台代码
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Width="100%"
BorderColor="#FF3300" Font-Size="Larger" >
<ItemTemplate>
<asp:image ID="Image1" runat="server" width="180" ImageUrl='<%#"cpimage/" +Eval("Name")%>' ToolTip='<%#Eval("Name").ToString().Replace(".jpg","")%>' />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Name").ToString().Replace(".jpg","")%>' ForeColor="Fuchsia" BackColor="#FF9900" Font-Size="Larger" CssClass="mid" BorderColor="Aqua" BorderStyle="Double"></asp:Label>
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:ListBox ID="ListBox1" runat="server" Height="300px" Width="200px">
</asp:ListBox>
DirectoryInfo imagesfile = new DirectoryInfo(Server.MapPath("./cpimage"));
DataList1.DataSource = imagesfile.GetFiles("*.jpg");
DataList1.DataBind();
<ItemTemplate>
//这里加上单击事件
<asp:image ID="Image1" runat="server" width="180" ImageUrl='<%#"cpimage/" +Eval("Name")% OnClick='Image_Click'>' ToolTip='<%#Eval("ToolTip").ToString().Replace(".jpg","")%>' />
//cs处理代码中添加
protected void Image_Click(object o,EventArgs e)
{
System.Web.UI.Image image=null;
if(o is System.Web.UI.Image)
image=o as System.Web.UI.Image;
/*DirectoryInfo imagesfile = new DirectoryInfo(Server.MapPath("./cpimage"));
List<string> files = imagesfile.GetFiles("*.jpg");
string fileFD=files.Where(x=>x==image.FilePath).First();
files=files.Where(x=>x!=image.FilePath).ToList();
files.Add(fileFD)
DataList1.DataSource=files.Select(x=>new {Name=x,ToolTip='Title' });
DataList1.DataBind();
*/
//上面的代码没有考虑imagesfile.GetFiles(string)返回类型 属于伪代码
}