本文介绍了动态地评估和演示在ASP.NET中设置图片的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET中的一个中继器的图像。我需要动态设置此图像的宽度从数据库返回的值。我从SQL数据库的信息,然后我绑定的中继结果集或数据源,我尝试在转发指定图像的宽度如下:

I have an image in a repeater in ASP.NET. I need to set the width of this image dynamically to a value returned from the database. I get the information from the SQL db, then I bind the repeater to the result set or datasource and I try to specify the width of the image in the repeater as follows:

<asp:Image ID="Image1" runat="server" Width='<%# Eval("ImageSize") %>' ImageUrl="~/Images/ProgressBar.jpg"/>

我得到一个错误,说明

I get an error stating

Specified cast is not valid.

难道这是因为正在从数据库返回的数据类型的原因引起的?

Could this be caused because of the datatype that is being returned from the db?

任何帮助是AP preciated。

Any help is appreciated.

谢谢
Ĵ

推荐答案

使用 System.Web.UI.WebControls.Unit.Parse 方法:

<asp:Image 
      ID="Image1" 
      runat="server" 
      Width='<%# System.Web.UI.WebControls.Unit.Parse(Eval("ImageSize").ToString()) %>'
      ImageUrl="~/Images/ProgressBar.jpg"/>

这篇关于动态地评估和演示在ASP.NET中设置图片的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:58