我们有一个非常简单的页面来显示记录。
基本上,它通过某些phoneno,如果发现它然后显示,并且有可能使用转发器的相同数字。这件事很好地没有问题但是现在通过介绍需要显示的照片配置文件来改变需求。
它的工作方式基本上是一旦返回记录,它根据特定文件夹上的PhoneID检查物理照片,如果它存在,则显示如“/images/profiles/1.jpg”,如果它不能然后找不到“/images/profiles/default.jpg”
我无法让它如何渲染。
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="IndexPage" %>
<form id="form1" runat="server">
<asp:label ID="lblErrMsg" runat="server" ></asp:label>
<asp:Panel runat="server" ID="pnlMainNoRec" Visible="false">
<table>
<tr>
<td>No record available.</td>
</tr>
</table>
</asp:Panel>
<asp:Panel runat="server" ID="pnlMainData" Visible="false">
<asp:Repeater id="rptrMain" runat="server">
<ItemTemplate>
<table>
<tr valign="top">
<td>
<table>
<tr>
<td><img alt="" src="/images/profiles/<%# DataBinder.Eval(Container.DataItem, "PhotoID") %>" /></td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td><p>First Name:</p></td>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
</tr>
<tr>
<td>Last Name:</td>
<td><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
</tr>
<tr>
<td>Status:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Status") %></td>
</tr>
<tr>
<td>Region:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Region") %></td>
</tr>
<tr>
<td>Address:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Address") %></td>
</tr>
<tr>
<td>Caller Type:</td>
<td><%# DataBinder.Eval(Container.DataItem, "CallerType") %></td>
</tr>
<tr>
<td>Program:</td>
<td><%# DataBinder.Eval(Container.DataItem, "ClientProgram")%></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
CS:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class IndexPage : System.Web.UI.Page
{
protected string thisConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected string thisQuery = "SELECT * FROM AllPhoneList WHERE PhoneNumber=@PhoneNumber";
protected string thisProfileLocation = "/images/profiles/";
protected void Page_Load(object sender, EventArgs e)
{
string phoneNo = Request.QueryString["PhoneNumber"];
GetDetail(phoneNo);
}
void GetDetail(string PhoneNumber)
{
try
{
using (SqlConnection conn = new SqlConnection(this.thisConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(thisQuery, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@PhoneNumber", SqlDbType.VarChar, 50);
cmd.Parameters["@PhoneNumber"].Value = PhoneNumber;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
int x = 0;
if (dt.Rows.Count > 0)
{
pnlMainData.Visible = true;
if (isFileExist(Int32.Parse(dt.Rows[x]["PhoneID"].ToString())))
???
//lblMainFirstName.Text = dt.Rows[0]["FirstName"].ToString();
//lblMainLastName.Text = dt.Rows[0]["LastName"].ToString();
rptrMain.DataSource = dt;
rptrMain.DataBind();
x+=1;
}
else
{
pnlMainNoRec.Visible = true;
}
}
}
catch (Exception err)
{
//lblErrMsg.Text = err.Message.ToString();
pnlMainNoRec.Visible = true;
}
}
public bool isFileExist(int phoneID)
{
string imageFolder;
imageFolder = Server.MapPath(thisProfileLocation) + phoneID.ToString();
if (File.Exists(imageFolder))
return true;
else
return false;
}
}
答案 0 :(得分:3)
变化:
<img alt="" src="/images/profiles/<%# DataBinder.Eval(Container.DataItem, "PhotoID") %>" />
要:
<img alt="" src="/images/profiles/<%# DataBinder.Eval(Container.DataItem, "PhotoID") %>" onerror="this.onerror=null;this.src='/images/profiles/default.jpg';" />
当图像返回404时,您将获得默认图像。