一个简单而又愚蠢的图片网址问题 [英] a big and simple even stupid question for image URL
问题描述
朋友,
我遇到了一个奇怪的问题:也许我错过了某事(我是Web开发的新手)
我发现遇到了一个非常简单的问题":只有一页显示图像和带有给定公共字符串的标签.
问题是标签正确无误,即〜/Books/Ajax/AJAX In Action.jpg",正好符合预期.但不显示图像.但是问题是,如果我直接给出
带有〜/Books/Ajax/AJAX In Action.jpg"的URL图像,则可以显示该图像.这里怎么了?
只需看下面的一个步骤:
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:
<%@ 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";
}
推荐答案
尝试
try
<img alt="" src="<%=Page.ResolveClientUrl(image_url_default)%>" />
或
or
<img alt="" src="<%=VirtualPathUtility.ToAbsolute(image_url_default)%>" />
如果您需要设置ASP.NET控件URL,请在页面加载中从后面的代码中完成.
if you need to set ASP.NET control URL, do it in Page load from code behind.
好吧,朋友们,一次又一次地搜索网站,我终于找到了解决方案,做不同的方式,这个问题就可以解决.但是,我仍然不明白为什么会出现原始问题或为什么原始方法不起作用.
如果有任何专家可以给我们一些提示,我会非常推荐.
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.
protected void Page_Load(object sender, EventArgs e)
{
this.ImageButton1.ImageUrl = image_url_default;
}
这篇关于一个简单而又愚蠢的图片网址问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!