简体   繁体   English

替换databinder.eval中的字符串

[英]Replace a string in databinder.eval

In my .aspx code i have the following element 在我的.aspx代码中,我具有以下元素

<asp:Image ID="GalleryImage" runat="server" ImageUrl='<%# Eval("ProductImage") %>'                                             />

The value returned for this is a image URl from a content delivery network with a sample url like 'http://cdn.xyz.com' 为此返回的值是来自内容交付网络的图像URl,其示例网址为'http://cdn.xyz.com'

I want to convert the url to 'https://cdn.xyz.com' 我想将网址转换为'https://cdn.xyz.com'

I tried to do ImageUrl='<%# Eval("ProductImage").Replace("http","https") %>' which doesnt seem to work. 我试图做ImageUrl='<%# Eval("ProductImage").Replace("http","https") %>'似乎不起作用。 Any ideas? 有任何想法吗?

You can handle it like: 您可以像这样处理它:

<%# ((string)Eval("ProductImage")).Replace("http", "https") %>

And if your string can be Null 如果您的字符串可以为Null

<%# ((string)Eval("ProductImage") ?? string.Empty).Replace("http", "https") %>

And it will be: 它将是:

<asp:Image ID="GalleryImage" runat="server" ImageUrl='<%# ((string)Eval("ProductImage") ?? string.Empty).Replace("http", "https") %>'

OR if you are sure your string will not be Null in any case. 或者,如果您确定您的字符串在任何情况下都不会为Null

<asp:Image ID="GalleryImage" runat="server" ImageUrl='<%# ((string)Eval("ProductImage")).Replace("http", "https") %>'

Try this, you might have to first convert to String for Replace to work: 尝试此操作,您可能必须先转换为String才能运行Replace

<asp:Image ID="GalleryImage" runat="server" ImageUrl='<%# Eval("ProductImage").ToString().Replace("http","https") %>'  

Eval returns object and Replace wouldn't work on object . Eval返回objectReplace不适用于object You need to Cast/Convert the returned object into String first and then use the Replace method on that String . 您需要先将返回的object Cast/ConvertString ,然后在该String上使用Replace方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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