日期:2014-05-17 浏览次数:20703 次
<asp:GridView ID="GridView1" runat="server" DataKeyNames="PhotoID" AllowPaging="True" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:ImageField DataImageUrlField="PhotoPath" HeaderText="缩略图"> </Columns> </asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Image x = e.Row.Cells[2].Controls[0] as Image; SetimageSize(x, 20); //高亮显示指定行 e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#336699';C=this.style.Color;this.style.Color='#ff0000'"); e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;"); //e.Row.Attributes.Add("onMouseOver", "C=this.style.Color;this.style.Color='#336699'"); //e.Row.Attributes.Add("onMouseOver", "Color=this.style.fontColor;this.style.fontColor='#000000'"); //删除指定行数据时,弹出询问对话框 ((LinkButton)(e.Row.Cells[3].Controls[0])).Attributes.Add("onclick", "return confirm('是否删除当前行数据!')"); } } public void SetimageSize(System.Web.UI.WebControls.Image Image, int MaxW) { SqlDataAdapter da = new SqlDataAdapter("select * from Photo where PhotoUser ='" + Session["UserName"].ToString() + "' order by PhotoID desc", cn); DataSet ds = new DataSet(); da.Fill(ds); string photoPath = ds.Tables[0].Rows[0][3].ToString(); //System.IO.File.Exists if (!File.Exists(Server.MapPath(photoPath))) { } else { System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(photoPath)); if (img.Width > MaxW) { int Nw = Convert.ToInt32((float)img.Width - ((float)img.Width - (float)150)); int Nh = Convert.ToInt32((float)img.Height / ((float)img.Width / (float)Nw)); Image.Width = Nw; } } //System.Drawing.Image img = System.Drawing.Image.FromFile(Page.Server.MapPath(Image.Src.Replace("..", ""))); }