学习Asp.net遇到一道作业题,要求创建一个Web应用程序,绑定图片的Width和Height属性,以控制图片的大小。我的想法是,在页面中放入两个TextBox控件及一个Button控件,一个Image控件显示图片。在两个TextBox控件分别输入一个整数值后,点击Button控件,Image控件的长度与宽度的属性值进行对应设置,图片显示因此发生相应变化。当我在第一个TextBox中输入一个值后,有如下实现代码:
 Int32 Width = Convert.ToInt32(TextBox1.Text);
 Int32 Height = Convert.ToInt32(TextBox2.Text);image控件的初始代码为:<asp:Image ID="Image1" runat="server" Height="196px" Width="285px" />现在我想,将Width和Height绑定到image控件中,用单值绑定,写了如下代码:<asp:Image ID="Image1" runat="server" Height='<%#Height+"px"%>' Width='<%#Width+"px"%>'/>提示错误,请问正确的代码应该怎样写,才能实现绑定?

解决方案 »

  1.   

     protected void Button1_Click(object sender, EventArgs e)
        {
            Image1.Width = Convert.ToInt32(TextBox1.Text.ToString().Trim());
            Image1.Height =Convert.ToInt32(TextBox2.Text.Trim().ToString());
        }
      

  2.   

     Height='<%#Height+"px"%>' Width='<%#Width+"px"%>'
    我是不清楚你这么搞有什么意义在方法外面定义两个全局变量吧s1 = TextBox1.Text.ToString().Trim();
      s2 =TextBox2.Text.Trim();
    绑值Height='<%=s1%>' Width='<%=s2%>'
      

  3.   

    试试
    private int _height=0, _width=0;public int Height
    {
        set{_height=value;}
        get{return _height;}
    }public int Width
    {
        set{_width=value;}
        get{return _width;}
    }protected void Button1_Click(object sender, EventArgs e)
      {
          Width = Convert.ToInt32(TextBox1.Text.Trim().ToString());
          Height =Convert.ToInt32(TextBox2.Text.Trim().ToString());
      }
    <asp:Image ID="Image1" runat="server" Height="<%=Height%>px" Width="<%=Width%>px"/>
      

  4.   

    谢谢楼上各位的指点,我试了多次,
    Height='<%=s1%>' Width='<%=s2%>'<asp:Image ID="Image1" runat="server" Height="<%=Height%>px" Width="<%=Width%>px"/>在visual studio 2008中都会提示出错。尽管我非常想用这种单值绑定的方式实现。
    无奈只好用Image1.Width = Convert.ToInt32(TextBox1.Text.ToString().Trim())实现,不知道这种单值绑定是否真能实现?如果能实现,在Image控件代码中究竟该怎样写?
      

  5.   

    等比例缩放图片
    <script language="JavaScript">
    var flag=false;
    function DrawImage(ImgD,iwidth,iheight){
      var image=new Image();
      image.src=ImgD.src;
      if(image.width>0 && image.height>0){
      flag=true;
      if(image.width/image.height>= iwidth/iheight){
      if(image.width>iwidth){   
      ImgD.width=iwidth;
      ImgD.height=(image.height*iwidth)/image.width;
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }
      else{
      if(image.height>iheight){   
      ImgD.height=iheight;
      ImgD.width=(image.width*iheight)/image.height;   
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }  }
    }  
    否则后台设置width
      

  6.   

    错误提示是
    无法从其“Width”属性的字符串表示形式“<%=Width %>px”创建System.Web.UI.WebControls.Unit”类型的对象。
    Height='<%=s1%>'的编译情况类似,请问控件属性如Image控件的属性究竟该怎样写,才能实现绑定?
      

  7.   


        protected void Button1_Click(object sender, EventArgs e)
        {
            Image1.Width = Convert.ToInt32(TextBox1.Text.ToString().Trim());
            Image1.Height = Convert.ToInt32(TextBox2.Text.Trim().ToString());
        }