【问题标题】:how to set asp.net image control width and height property如何设置asp.net图像控件的宽高属性
【发布时间】:2017-04-12 15:39:01
【问题描述】:

我正在使用 asp.net 和 c# 我想使用 c# 代码设置图像的高度和宽度属性,我可以做到,但该值将设置为内联样式 像这样:

<img id="image1" style="height:220px;width:800px;border-width:0px;">

但我想要这样的东西

<img id="image1" width="800" height="220">

我使用这个代码,但它呈现像 css 代码:(

  image1.Width = 800;
  image1.Height =220;

有什么想法吗?

【问题讨论】:

    标签: image set height width


    【解决方案1】:

    只需将其定义为属性:

    image1.Attributes.Add("width", "800");
    image1.Attributes.Add("height", "220");
    

    【讨论】:

      【解决方案2】:

      您必须使用Unit.Pixel() 方法。所以在你的情况下:

      image1.Width = Unit.Pixel(800);
      image1.Height = Unit.Pixel(220);
      

      【讨论】:

        【解决方案3】:

        如果你使用

        <asp:Image ID="Image1" runat="server"  Width="800" Height="220" />
        

        而不是

        <img id="image1" width="800" height="220">
        

        应该可以正常使用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-27
          • 1970-01-01
          • 2013-12-21
          相关资源
          最近更新 更多