views:

121

answers:

3

Hi All,

Quick question here. I'm sure it's possilbe, just can't get it to work. I've got a gridview. In have a gridview. The gridview is bound to a list of my custom class. The class exposes a link to an image as well as the image's height and width. I have a Image Control in the gridview. I've bound the Image Url to the correct property. Now, I'd like to bind the height and width properties as well. But everytime I do that I get the following error:

Cannot create an object of type 'System.Web.UI.WebControls.Unit' from its string representation '"<%#Bind("GetImageHeight()")%' for the 'Height' property.

Here is an example of the tag:

<asp:Image runat="server" ID="imgProduct" ImageUrl='<%#Bind("ImageUrl")%>' 
     Height="<%#Bind("GetImageHeight()")%>" Width="<%#Bind("GetImageWidth()")%>">
</asp:Image>

So, in short... how to I bind properties which are not string?

Sample of methods GetImageHeight and GetImageWidth:

   public Unit GetImageHeight()
    {
        Unit u = new Unit(Convert.ToString(ImageHeight) + "px");
        return u;
    }

    public Unit GetImageWidth()
    {
        Unit u = new Unit(Convert.ToString(ImageWidth) + "px");
        return u;
    }
A: 

Can you change your GetImageHeight/Width() methods to return a Unit?

womp
+1  A: 

DOH! I missed it first time around.. Try dropping the quotes from your binding

 Height="<%#Bind("GetImageHeight()")%>"

should look like

 Height="<%#Bind(GetImageHeight())%>"

and returning an int should work just fine. no need for all the busywork in the methods.

I could be wrong...

Sky Sanders
It returns a unit, still doesn't work...
vondip
A: 

This works on my end, give it a shot:

<asp:Image runat="server" ID="imgProduct" ImageUrl='test.jpg' 
 Height='<%# GetImageHeight() %>' Width='<%# GetImageWidth() %>' />

In the .cs:

protected int GetImageHeight()
{
    return 5;
}
protected int GetImageWidth()
{
    return 5;
}

Render this:

<img id="imgProduct" src="test.jpg" style="height:5px;width:5px;border-width:0px;" />
Nick Craver
but than it's not bound to the underlined object. I need to still have it bound, the object is holding that information. not the page... Thank you...
vondip