Reputation: 14790
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.
Upvotes: 1
Views: 1220
Reputation: 9129
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).
Upvotes: 1
Reputation: 5318
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>
Upvotes: 4