开发者

Why can't the attribute imageurl in the image tag not accept an absolute path?

I'm using two image tags. In one the used path is relative and in the other it is absolute. The absolute path image is not shown. Here is the code:

Expt_Image2.aspx page:

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

<!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Image
                ID="Image1"
                runat="server"
                ImageUrl="~/Image/Bluehills.jpg"
                He开发者_如何学Goight="100"
                Width="100"/>
            <asp:Image
                ID="Image2"
                runat="server"
                ImageUrl="C:\Documents and Settings\Lovey\My Documents\Visual Studio 2008\WebSites\Expt-New\Image\Sunset.jpg"
                Height="100"
                Width="100"/>
        </div>
        </form>
    </body>
</html>

Expt_Image2.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class Expt_Image2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles(MapPath("~/Image/"));
        Image im = new Image();
        im.ImageUrl = files[1];
        im.AlternateText = files[1];
        form1.Controls.Add(im);
    }
}


The first error that in your Page_Load method you get your image and store it in array. In C# indexes of items in arrays start from 0, not from 1.

Second, you must specify the virtual path to the property ImageUrl of the Image control.

It is the right Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
    string[] files = Directory.GetFiles( MapPath( "~/Image/" ) );
    Image im = new Image();
    im.ImageUrl = "~/Image/" + Path.GetFileName( files[ 0 ] );
    im.AlternateText = files[ 0 ];
    im.Height = 100;
    im.Width = 100;
    form1.Controls.Add( im );
}


Doesn't MapPath return the physical location of the file. In your case something like C:\inetpub\wwwroot\mysite\image

Whereas you would want the virtual path eg. http://mysite/image/image1.png

Something like this should work:

    DirectoryInfo di = new DirectoryInfo(MapPath("~/Image/"));
    FileInfo[] files = di.GetFiles();        

    Image im = new Image();
    im.ImageUrl = "~/Image/" + files[0].Name;
    im.AlternateText = "~/Image/" + files[0].Name;
    form1.Controls.Add(im);


Use the application path,

Request.ApplicationPath + "~/Image/"

This will fix the problem.

One more thing. You don't need to put "~" before the path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜