简体   繁体   中英

multiple statments in inline c# on and asp.net page

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"/>

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM