是呀,顶!
datalist一行四列显示
datalist一行四列显示
解决方案 »
- 如何动态设置GridView单元格背景颜色
- treeview如何限制checkbox的选择?
- default.aspx里没有连接数据库的语句,请问会在哪啊?
- 请帮忙,调用某个项目中的方法出错了
- IF EXISTS(SELECT 1 FROM #class WHERE obj_id =0) 这句是什么意思啊?
- ADO.NET得问题
- 两个网页之间传送字符串的问题
- 如何才能让按钮只能按一次,下次再按就不起作用了
- 【急】MVC中,如何将一个分部页面(index)嵌套到action view (Home/Defult)中?
- 菜鸟问题,如何在页面通过点击一个键打开另一个页面(原页面不卸载)。
- web控件库里面调用form?怎么什么都看不见?
- 有难度:怎样轮流显示图片(若解决了,追加80分)
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;
}