我做了三个页面三个页面中都有个 IMAGE 控件.  现在我写了个 事件 
  protected void imgpre_show(object sender, EventArgs e)
    {
        string imgurl = this.Imgpre.ImageUrl;
        Bitmap imagebmp;
        char x = imgurl[0];
        try
        {
            if (x.ToString() == "h")
            {
                WebRequest wr = WebRequest.Create(imgurl);
                WebResponse res = wr.GetResponse();
                imagebmp = new Bitmap(res.GetResponseStream());
            }
            else
            {
                imagebmp = new Bitmap(Server.MapPath(imgurl));
            }
            Single k = imagebmp.Width / imagebmp.Height;            this.Imgpre.Width = imagebmp.Width;
            this.Imgpre.Height = imagebmp.Height;
            if (k > 1)
            {
                if (imagebmp.Width > 100)
                {
                    this.Imgpre.Width = 100;
                    this.Imgpre.Height = Convert.ToInt32(100 / k);
                }
            }
            else
            {
                if (imagebmp.Height > 100)
                {
                    this.Imgpre.Height = 100;
                    this.Imgpre.Width = Convert.ToInt32(100 / k);
                }
            }
        }
        catch
        {
        }
    }目的是在控件显示图像前根把图像按比例缩放到 100PX 之内. 在单独页面运行没什么问题.
现在的问题是, 三个页都有这个控件,所以我想把这个事件定义到 APP_CODE 里的CS文件里
运行时提示错误.1  不存在 this.Imgpre 的定义. 这个可以理解.毕竟不在页上.但是怎么改这一块?2  当前或上下文中 不存在 SERVER      是指这一句
  Server.MapPath         在CS文件中不能调用 服务端方法么?求指点

解决方案 »

  1.   

    System.Web.HttpContext.Current.Server.MapPath 
    string imgurl;
    可以作为类的属性对待,用的时候赋值
      

  2.   

    现在想到的方法是写个函数,把 IMGURL 作为字符串传入, 计算 长 和 高后再传回但是这样三个页面各自都需要一个 针对IMAGE控件的 事件代码非得这样不可么?
      

  3.   

    string imgurl = this.Imgpre.ImageUrl;
                  ----------这一段倒也罢了.主要是这里
    this.Imgpre.Width = imagebmp.Width;
    this.Imgpre.Height = imagebmp.Height;因为我算完后就对控件 的长宽赋值 
    如果非要当前页面才能赋值.就郁闷了.
      

  4.   

    如果是这样,你可以考虑继承Image控件做一个自己的控件,把你这个处理函数加上去,然后要用的地方就用继承控件。