Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a string in databinder.eval

In my .aspx code i have the following element

<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'

I want to convert the url to 'https://cdn.xyz.com'

I tried to do ImageUrl='<%# Eval("ProductImage").Replace("http","https") %>' which doesnt seem to work. Any ideas?

like image 262
Dominick Avatar asked Aug 31 '25 03:08

Dominick


2 Answers

You can handle it like:

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

And if your string can be 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.

<asp:Image ID="GalleryImage" runat="server" ImageUrl='<%# ((string)Eval("ProductImage")).Replace("http", "https") %>'
like image 199
Afnan Ahmad Avatar answered Sep 02 '25 16:09

Afnan Ahmad


Try this, you might have to first convert to String for Replace to work:

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

Eval returns object and Replace wouldn't work on object. You need to Cast/Convert the returned object into String first and then use the Replace method on that String.

like image 35
sachin Avatar answered Sep 02 '25 16:09

sachin