读书人

ImageField不具有id公共属性,如何使图

发布时间: 2012-06-01 16:46:36 作者: rapoo

ImageField不具有id公共属性,怎么使图片按比例缩小?
一般的限制长宽会使图片变性的, 要按比例才好, 不过ImageField没有id属性, 网上找的这个办法就不行了.
有没有别的解决办法?


C# code
        protected void Page_Load(object sender, EventArgs e)        {            if (Session["UserName"] == null)                Response.Redirect("../index.aspx");            if (!IsPostBack)            {                this.PhotoDataBind();                SetimageSize(RroImage, 950);            }        }public void SetimageSize(System.Web.UI.HtmlControls.HtmlImage Image, int MaxW){System.Drawing.Image img = System.Drawing.Image.FromFile(Page.Server.MapPath(Image.Src.Replace("..", "")));if (img.Width > MaxW){int Nw = Convert.ToInt32((float)img.Width - ((float)img.Width - (float)570));int Nh = Convert.ToInt32((float)img.Height / ((float)img.Width / (float)Nw));Image.Width = Nw;Image.Height = Nh;Image.Alt = String.Format("图片自动缩小 Size:{0}×{1}", Nw, Nh);} }


photo.aspx.cs
HTML code
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="PhotoID" >                                                <Columns>                                                    <asp:BoundField DataField="PhotoID" HeaderText="ID" />                                                                                                                                                  <asp:BoundField DataField="PhotoUser" HeaderText="用户" />                                                    <asp:ImageField DataImageUrlField="PhotoPath" HeaderText="缩略图"/>                                                    <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />                                                    <%--Field不具有id属性--%>                                                </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;
x.ImageUrl = "http://dotnet.aspx.cc/Images/logoSite.gif";
SetimageSize(x, 20);
}
}

public void SetimageSize(System.Web.UI.WebControls.Image Image, int MaxW)

适当修改

读书人网 >asp.net

热点推荐