日期:2014-05-17  浏览次数:20409 次

图片以相对路径存在数据库中 用ASP.NET 在页面中根据输入的字显示图片
图片是以../Images/images/a/....存在数据库中的
前台
<body>
  <form id="form1" runat="server">
  <div>
   
  <asp:TextBox ID="TextBox1" runat="server" Height="21px"></asp:TextBox>
  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查找" />
  </div>
  <li><a ><asp:Image width="168" height="103" runat="server" id="Image1"/></a>
  <b><a ></a></b>
  </li
  </form>
</body>

后台:
public partial class _Default : System.Web.UI.Page 
{
  string connectionstring = ConfigurationManager.ConnectionStrings["ayyx"].ConnectionString;
  protected void Page_Load(object sender, EventArgs e)
  {
   
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
   
   
  string name = this.TextBox1.Text.Trim();
  SqlConnection con = new SqlConnection(connectionstring);
  string cmdText = "select ImageUrl from font1 where ImageName='" + name + "'";
  SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
  DataSet ds = new DataSet();
  da.Fill(ds);
  con.Open();
   
  this.Image1.ImageUrl = ds.Tables[0].Rows[0]["ImageUrl"].ToString();  
   
  }
}

但是不知道为什么图片出不来 请帮忙看看问题出在哪里 谢谢了

------解决方案--------------------


记得 tempInfo 的数据 是从后台获取的哦 别忘记了
------解决方案--------------------
..是父目录了?

估计是路径问题。

先把"../Images/images/a/..." 赋值给this.Image1.ImageUrl,试一试这种相对路径是不是可以。

如果可以,再单步调试, 看看ds.Tables[0].Rows[0]["ImageUrl"].ToString();是不是正确的值。
------解决方案--------------------
搞清楚你这个Images文件夹是在根目录的那个地方。
如果是在根目录下面,你读取出来的路径就应该替换掉前面的"../"
this.Image1.ImageUrl = ds.Tables[0].Rows[0]["ImageUrl"].ToString().Replace("../",""); 

打开页面,看看HTML源码是什么样子的。再对应你网站的Images目录结构
------解决方案--------------------
既然图片是以../Images/images/a/....存在数据库中的
为什么还要“让数据库中的“\”变成“/”呢? ”

不是可以用replace("\","/") 吗?