Home » Asp.netRSS

error message is "Cannot convert type 'string' to 'System.Web.UI.WebControls.Unit"

<asp:ImageID="Image1"ImageUrl='<%# Eval("GalleryFullSizeName", "~/{0}") %>'Width='<%# Eval("GalleryImageWidth") + px %>'Height=500runat="server"/>

 

16 Answers Found

 

Answer 1

You need to provide px as a string  literal: 

<asp:Image ID="Image1" ImageUrl='<%# Eval("GalleryFullSizeName", "~/{0}") %>' Width='<%# Eval("GalleryImageWidth") + "px" %>' Height=500 runat="server"/>

Or if you use the format syntax:

<asp:Image ID="Image1" ImageUrl='<%# Eval("GalleryFullSizeName", "~/{0}") %>' Width='<%# Eval("GalleryImageWidth", "{0}px") %>' Height=500 runat="server"/>
   
 

Answer 2

When I changed the code according to your suggestion, still same error  message.

 

Answer 3

Width='<%# Unit.Pixel(Eval("GalleryImageWidth")) %>' --> is it possible to do it this way?

 

Answer 4

When I try to use Width='<%# Unit.Pixel(Eval("GalleryImageWidth")) %>'

the error message  changed to : 'System.Web.UI.WebControls.Unit.Pixel(int)' has some invalid arguments. Please help! thank you so much!

 

Answer 5

Do you need to have the px?

What's the value on GalleryImageWidth? Is it a numerical value?

 

Answer 6

 i build CSS dynamically from database too for some elements

and i go like this

width="<%=unit(convert.toInt32(dr("mywidth"))%>" height="<%=unit(convert.toInt32(dr("mywidth"))%>" />

although you may need to jiggle a little for your needs

as i never code in html pages like this only in code behind wich would be a little different 

i also imports or using system.web.ui.webcontrols

 

p.s also like above remove the PX this is only on style sheets or inlayed CSS 

 

 


 

 

Answer 7

This is answer for johram  :

 

Do you need to have the px?

What's the value on GalleryImageWidth? Is it a numerical value?

I don't think I really need px, GalleryImageWidth just a numerical value. The data type  is INT.

 

Answer 8

Then, what if you just do:

Width='<%# Eval("GalleryImageWidth") %>'
  
 

Answer 9

hi, did you declare GalleryImageWidth as int data type  in the database?

 

Answer 10

Then, what if you just do:

Width='<%# Eval("GalleryImageWidth") %>'
 When I only use Width='<%# Eval("GalleryImageWidth") %>'
Error message  is :System.InvalidCastException: Specified cast is not valid.

 

 

Answer 11

did you declare GalleryImageWidth as int data type  in the database?


In the SQL 2005: GalleryImageWidth :DATA TYPE: int, SYSTEM TYPE: int, LENGTH: 4

 

Answer 12

Width='<%# Unit.Pixel("GalleryImageWidth")%>'  --> how about like this?

 

Answer 13

Width='<%# Unit.Pixel("GalleryImageWidth")%>'  and Width='<%# Unit.Pixel(Eval("GalleryImageWidth")) %>'

They have the same error  message:: The best overloaded method match for 'System.Web.UI.WebControls.Unit.Pixel(int)' has some invalid arguments.

Lord, I really need help to finish this page, please give the wisdom to people here.

 

 

Answer 14

KEEP IT SIMPLE

 

eITHER

use 

Width='<%#Convert.ToString(GalleryImageWidth)>'   Width='<%#Convert.ToString(GalleryImageWidth) %>'

Or use

 

Width='<%#Convert.ToInt32(GalleryImageWidth)>'   Width='<%#Convert.ToInt32(GalleryImageWidth) %>'

 

as it is written there for both the width and the height removed the and and the eval and the unit.pixel
as far as i know you only use unit.pixel etc when you are in code behind and your trying to get say the width and height of an image on disk

or finally you can have the whole lot as a string  in code beind and just <%#mystylestring%>

 

 

Answer 15

uhm, i agreed, that should do the magic. In fact I think Width='<%#GalleryImageWidth%>' should work if GalleryImageWidth is type  int.

 

Answer 16

try it Width='<%# Unit.Pixel(Convert.ToInt32(Eval("GalleryImageWidth"))) %>'


This worked fine for me

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter