Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, friends,

I have met a strange problem: perhaps I have missed sth (I am new to web development)

I found I have met a very simple "problem": just one page to display a image and a label with a given public string.
the problem is that the label out is correct, i.e. "~/Books/Ajax/AJAX In Action.jpg",just as expected. but the image is not displayed. But problem is that if I directly give the
URL image with "~/Books/Ajax/AJAX In Action.jpg", then the image can be displayed. what is wrong here?

see below just one step:

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Server.UrlEncode(Eval("image_url_default").ToString())%> '/>
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"> <% =image_url_default%>
    </form>
</body>
</html>

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    public string image_url_default="~/Books/Ajax/AJAX In Action.jpg";
}
Posted
Updated 2-Sep-12 6:13am
v4
Comments
DamithSL 23-Jun-12 13:29pm    
How you set image url? can update the question with code sample?
Seraph_summer 24-Jun-12 7:10am    
thanks, I have made a simple example, see the reply to pascal Ganaye.

thanks for your kind help!
Pascal Ganaye 23-Jun-12 17:19pm    
your logic seems right.
You should make a simple page and if it still do it post it here.
Seraph_summer 24-Jun-12 9:37am    
I have simplified it again, I found I have met a very simple "problem":

see below just one step:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Server.UrlEncode(Eval("image_url_default").ToString())%> '/>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"> <% =image_url_default%>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
public string image_url_default="~/Books/Ajax/AJAX In Action.jpg";
}


the problem is:

above two places to call the same variable "image_url_default", one is for image URL and the other is direclty output into a label, the lable output is correct, i.e. "~/Books/Ajax/AJAX In Action.jpg", but the image URL is not correct. I think there there is a simple or even stupid mistake done by myself, but I do not know what:)

try

C#
<img alt="" src="<%=Page.ResolveClientUrl(image_url_default)%>" />


or

C#
<img alt="" src="<%=VirtualPathUtility.ToAbsolute(image_url_default)%>" />


if you need to set ASP.NET control URL, do it in Page load from code behind.
 
Share this answer
 
v3
Comments
Seraph_summer 24-Jun-12 15:48pm    
you are right, your proposal works for html img. this is also one solution.
thanks!
OK, friends, by searching website again and again, I have found finally the solution,by doing different way, this problem can be solved. but still, I do not understand why original problem occurs or why original way does not work.
I appriciated it very much If any expert can give us some hints.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        this.ImageButton1.ImageUrl = image_url_default;
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900