是呀,顶!
datalist一行四列显示

解决方案 »

  1.   

    动态生成表格,取得图片的高度和宽度,
    private void ShowImg(string path,string path2)
    {
    this.tImg.Rows.Clear();
    TableCell tc = new TableCell();
    TableRow tr = new TableRow(); //图片排列的列数
    int cols = 4;
    //显示图片的高度和宽度
    int ImgWidth = 140;
    int ImgHeight =140;
    //缩放倍数
    decimal TimesW,TimesH; //图片的实际高度和宽度
    int TrueWidth,TrueHeight; //名称索引
    int index1;
    int index2; //缩略后的宽度和高度
    int LastWidth,LastHeight;
    System.Web.UI.WebControls.ImageButton MyImage;
    //图片名称
    string ImgName;
    string[] ImgFiles = Directory.GetFiles(Server.MapPath(path));
    for(int i = 0;i<ImgFiles.Length;i++)
    {
    tc = new TableCell();
    tc.Height = ImgHeight;
    tc.Width = ImgWidth;
    tc.HorizontalAlign = HorizontalAlign.Center;
    tc.VerticalAlign = VerticalAlign.Middle;
    //取得图片的高度和宽度
    System.Drawing.Image img = System.Drawing.Image.FromFile(ImgFiles[i]); //实际高度和宽度
    TrueWidth = img.Width;
    TrueHeight = img.Height;
    //高度和宽度都在控制范围内
    if(TrueWidth<=ImgWidth && TrueHeight<=ImgHeight)
    {
    //不设定高度和宽度
    index1 = ImgFiles[i].LastIndexOf("\\")+1;
    index2 = ImgFiles[i].Length;
    ImgName = ImgFiles[i].Substring(index1,index2-index1);
    MyImage = new ImageButton();
    MyImage.ImageUrl = path2+"/"+ImgName;
    MyImage.Click += new System.Web.UI.ImageClickEventHandler(ImgClick);
    //MyImage.Click += new EventHandler(ImgClick);
    }
    else
    {
    if(TrueWidth<=ImgWidth && TrueHeight>ImgHeight)
    {
    index1 = ImgFiles[i].LastIndexOf("\\")+1;
    index2 = ImgFiles[i].Length;
    ImgName = ImgFiles[i].Substring(index1,index2-index1);
    MyImage = new ImageButton();
    MyImage.ImageUrl = path2+"/"+ImgName;
    MyImage.Height = ImgHeight;
    //MyImage.Click += new EventHandler(ImgClick);
    MyImage.Click += new System.Web.UI.ImageClickEventHandler(ImgClick);
    }
    else
    {
    if(TrueWidth>ImgWidth && TrueHeight<=ImgHeight)
    {
    index1 = ImgFiles[i].LastIndexOf("\\")+1;
    index2 = ImgFiles[i].Length;
    ImgName = ImgFiles[i].Substring(index1,index2-index1);
    MyImage = new ImageButton();
    MyImage.ImageUrl = path2+"/"+ImgName;
    MyImage.Width = ImgWidth;
    //MyImage.Click += new EventHandler(ImgClick);
    MyImage.Click += new System.Web.UI.ImageClickEventHandler(ImgClick);
    }
    else
    {
    //计算转换后的高度和宽度
    TimesW = (TrueWidth/ImgWidth)+1;
    TimesH = (TrueHeight/ImgHeight)+1;
    if(TimesW>TimesH)
    {
    LastWidth = Convert.ToInt32(TrueWidth/TimesW);
    LastHeight = Convert.ToInt32(TrueHeight/TimesW);
    }
    else
    {
    LastWidth = Convert.ToInt32(TrueWidth/TimesH);
    LastHeight = Convert.ToInt32(TrueHeight/TimesH);
    }
    index1 = ImgFiles[i].LastIndexOf("\\")+1;
    index2 = ImgFiles[i].Length;
    ImgName = ImgFiles[i].Substring(index1,index2-index1);
    MyImage = new ImageButton();
    MyImage.ImageUrl = path2+"/"+ImgName;
    MyImage.Height = LastHeight;
    MyImage.Width = LastWidth;
    //MyImage.Click += new EventHandler(ImgClick);
    MyImage.Click += new System.Web.UI.ImageClickEventHandler(ImgClick);
    }
    }
    }//else
    tc.Controls.Add(MyImage);
    tr.Cells.Add(tc);
    if(i % cols == (cols-1))
    {
    this.tImg.Rows.Add(tr);
    tr = new TableRow();
    } }//for
    if(ImgFiles.Length%cols>0)
    {
    this.tImg.Rows.Add(tr);
    }
    }//private 
    private void ImgClick(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    for(int i = 0;i<this.tImg.Rows.Count;i++)
    {
    for(int j=0;j<this.tImg.Rows[i].Cells.Count;j++)
    {
    this.tImg.Rows[i].Cells[j].BorderWidth=1;
    this.tImg.Rows[i].Cells[j].BorderColor = System.Drawing.Color.LightGray;
    }
    }
    string name;
    ((TableCell)((System.Web.UI.WebControls.ImageButton)sender).Parent).BorderColor = System.Drawing.Color.DarkBlue;
    ((TableCell)((System.Web.UI.WebControls.ImageButton)sender).Parent).BorderWidth = 2;
    name = ((System.Web.UI.WebControls.ImageButton)sender).ImageUrl;
    this.tbSelect.Value = name;

    }