替换databinder.eval中的字符串

Sco*_*now 1 c# asp.net webforms

在我的.aspx代码中,我有以下元素

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

为此返回的值是来自内容传送网络的图像UR1,其具有样本URL 'http://cdn.xyz.com'

我想将网址转换为 'https://cdn.xyz.com'

我试着这样做ImageUrl='<%# Eval("ProductImage").Replace("http","https") %>'似乎不起作用.有任何想法吗?

Afn*_*mad 8

您可以像下面这样处理它:

<%# ((string)Eval("ProductImage")).Replace("http", "https") %>
Run Code Online (Sandbox Code Playgroud)

如果你的字符串可以 Null

<%# ((string)Eval("ProductImage") ?? string.Empty).Replace("http", "https") %>
Run Code Online (Sandbox Code Playgroud)

它将是:

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

或者如果你确定你的字符串Null在任何情况下都不会.

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