如何设置ASP.NET图像控制宽度和高度属性



我正在使用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;

有什么想法吗?

只是将其定义为属性:

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

如果使用

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

而不是

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

它应该正常工作

您必须使用Unit.Pixel()方法。因此,在您的情况下:

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

最新更新