1

I have the following line in my aspx file:

<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>' Height="114" Width="152"/>

Is it possible to add another line to the inline c# something like this?

<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem); SetImageSize(this) %>' Height="114" Width="152"/>

2 Answers 2

2

I am afraid that this is not possible. But you could write another method on this helper class which will invoke the two operations at once.

<asp:Image 
    ID="Image1" 
    runat="server" 
    ImageUrl='<%# MediaHelper.GetMediaUrlAndSetImageSize(Container.DataItem, this) %>' 
    Height="114" 
    Width="152"
/>

Also mixing C# code with ASPX might lead to spaghetti. I would tend to avoid it as much as possible.

Sign up to request clarification or add additional context in comments.

4 Comments

cool thanks! yeah this code is really messy. It was written that way by the previous developer and we dont have time to go through and fix it so I just have to get what i have working... How can i pass the image to the method. I just noticed passing "this" is the wrong object..
@MBU, which image? Is it part of the current data bound item? Container.DataItem might be sufficient as argument in this case.
i think so. basically i want to method to return the imageurl and at the same time i want to dynamically set the height and width of the image. Is that possible?
@MBU, you could also use the DataBinding syntax <%# %> to set the Width and Height properties.
1

You could use multiple method calls to accomplish what you are trying to do:

<asp:Image 
    ID="Image1" 
    runat="server" 
    ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>' 
    Height="<%# MediaHelper.GetMediaHeight(Container.DataItem) %>" 
    Width="<%# MediaHelper.GetMediaWidth(Container.DataItem) %>"
/>

Or just bind an object to the control that has all of those values exposed as properties.

2 Comments

what would the return type have to be? string?
In the example above, the return type for GetMediaUrl would be string. The return type for GetMediaHeight and GetMediaWidth would be int (or possibly System.Web.UI.WebControls.Unit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.