1

I have this line:

<asp:HyperLink ID="awsImage" runat="server" Target="_blank" 
ImageUrl='<%# Eval("ImageURL") %>' Width='<%# Eval("ImageWidth").ToString() %>'
Height='<%# Eval("ImageHeight").ToString() %>' ></asp:HyperLink>

And I get this error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0030: Cannot convert type 'string' to 'System.Web.UI.WebControls.Unit'

I also have tried simple Eval without .ToString but I get this error: This is the exact error I get if I use the Eval without .ToString

Exception Details: System.InvalidCastException: Specified cast is not valid.

4

2 回答 2

4

use this

<asp:HyperLink ID="awsImage" runat="server" Target="_blank" 
ImageUrl='<%# Eval("ImageURL") %>' 
Width='<%# Unit.Pixel(Convert.ToInt32(Eval("ImageWidth"))) %>'
Height='<%# Unit.Pixel(Convert.ToInt32(Eval("ImageHeight"))) %>' ></asp:HyperLink>
于 2013-01-03T22:31:17.157 回答
1

I checked on MSDN. Height and Width are of type Unit:

Width='<%# new Unit((int)Eval("ImageWidth")) %>' 
Height='<%# new Unit((int)Eval("ImageHeight")) %>'

or by using the static method Unit.Pixel

Width='<%# Unit.Pixel((int)Eval("ImageWidth")) %>' 
Height='<%# Unit.Pixel((int)Eval("ImageHeight")) %>'

Because the expression is evaluated on the server, you have to provide the correct data type (the compiler generates the code).

于 2013-01-03T22:30:03.577 回答