在ASP.NET Web窗体网站上存储和显示图像

时间:2015-07-06 10:36:44

标签: c# sql asp.net image webforms

我正在建设的网站拥有571间房屋,其中包含每栋房屋的信息。每个房子都有一个与之相关的图像。该信息来自名为houses的SQL数据库表。我使用自定义寻呼机来过滤571个房屋,每页5个结果。我正在使用转发器控件来执行此操作,因此我没有多个aspx页面。我的问题是我想将571个图像的文件夹存储在一个位置(存储在网络驱动器上),并在数据库表格列中为每个房子设置一个指向该文件夹中特定图像的路径,并显示每个图像。使用中继器控制时的房子。我看了很多教程,但没有什么能帮助我。请不要发布教程的任何链接,因为我已经查看了所有链接。如果你之前做过这样的事情,请发表你的经验,因为这对我来说是新的。源代码如下。

数据库结构

[Id]          NCHAR (10)     NOT NULL,
[Name]        NVARCHAR (MAX) NULL,
[Townland]    NVARCHAR (MAX) NULL,
[Near]        NVARCHAR (MAX) NULL,
[Status]      NVARCHAR (MAX) NULL,
[Built]       NVARCHAR (MAX) NULL,
[Description] NVARCHAR (MAX) NULL,
[Families]    NVARCHAR (MAX) NULL,
[Image]       VARCHAR (200)  NULL,
CONSTRAINT [PK_Houses] PRIMARY KEY CLUSTERED ([Id] ASC)

示例表数据

enter image description here

Houses.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#expanderHead").click(function () {
                $("#expanderContent").slideToggle();
                if ($("#expanderSign").text() == "+") {
                    $("#expanderSign").html("-")
                }
                else {
                    $("#expanderSign").text("+")
                }
            });
        });
    </script>
    <script>
        $(function () { setCurrentTab('tab2'); });
    </script>
    <div class="box">
        <div>
            <div class="body">
                <h1>Search Houses</h1>

                <p>
                    Welcome to Houses of Mayo search page. Enter details below to search for a specific house. Additionally you can use advanced search or search by map.
                </p>
                <div>
                    <form style="margin-left: 32%">
                        Name of House:
                        <input type="search" placeholder="Search" style="margin-left: 7%">
                    </form>
                    <br />
                    <form style="margin-left: 32%">
                        Townland:
                        <input type="search" placeholder="Search" style="margin-left: 14%">
                    </form>
                    <br />
                    <form style="margin-left: 32%">
                        Near:
                        <input type="search" placeholder="Search" style="margin-left: 20%">
                    </form>
                    <br />
                    <form style="margin-left: 32%"><a id="expanderHead" style="cursor: pointer;">Advanced Search</a><input type="button" value="Search" class="button" style="margin-left: 35%" /></form>
                    <div id="expanderContent" style="display: none">
                        <br />
                        <form style="margin-left: 32%">
                            Associated Families:
                            <input type="search" placeholder="Search" style="margin-left: 2%">
                        </form>
                        <br />
                        <form style="margin-left: 32%">
                            Keyword:
                            <input type="search" placeholder="Search" style="margin-left: 15%">
                        </form>
                        <br />
                    </div>
                </div>
                <br />
                <br />
                <br />

                <h1>Houses By Alphabetical Order</h1>

                <ul id="rooms">
                    <asp:Repeater ID="rptData" runat="server">
                        <ItemTemplate>
                            <li>
                                <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                    <img src="" alt="img" width="398" height="287"/></a>
                                <h2>
                                    <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                        <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label></a></h2>
                                <p>
                                    <b>ID: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                                    <br />
                                    <b>Name of House: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                    <br />
                                    <b>Townland: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Townland") %>'></asp:Label>
                                    <br />
                                    <b>Near: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Near") %>'></asp:Label>
                                    <br />
                                    <b>Status/Public Access: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Status") %>'></asp:Label>
                                    <br />

                                    <b>Date Built: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Built") %>'></asp:Label>
                                </p>
                            </li>
                        </ItemTemplate>
                    </asp:Repeater>
                </ul>
                <asp:Repeater ID="rptPager" runat="server">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                            Style="padding: 8px; margin: 2px; background: #ac9e94; border: solid 1px #666; font: 8pt; color: #594334; display: inline-block;"
                            CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
                            OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </div>
    </div>
</asp:Content>

Houses.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Houses_of_Mayo.images
{
    public partial class Houses : System.Web.UI.Page
    {
        private int PageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetHousesPageWise(1);
            }
        }

        private void GetHousesPageWise(int pageIndex)
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("GetHousesPageWise", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
                    cmd.Parameters.AddWithValue("@PageSize", PageSize);
                    cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
                    cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                    con.Open();
                    IDataReader idr = cmd.ExecuteReader();
                    rptData.DataSource = idr;
                    rptData.DataBind();
                    idr.Close();
                    con.Close();
                    int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
                    this.PopulatePager(recordCount, pageIndex);
                }
            }
        }

        private void PopulatePager(int recordCount, int currentPage)
        {
            double dblPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
            int pageCount = (int)Math.Ceiling(dblPageCount);
            List<ListItem> pages = new List<ListItem>();
            if (pageCount > 0)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
                }
            }
            rptPager.DataSource = pages;
            rptPager.DataBind();
        }

        protected void Page_Changed(object sender, EventArgs e)
        {
            int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
            this.GetHousesPageWise(pageIndex);
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }
    }
}

存储过程

CREATE PROCEDURE GetHousesPageWise
      @PageIndex INT = 1
      ,@PageSize INT = 5
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [Name] ASC
      )AS RowNumber
      ,[Id]
      ,[Name]
      ,[Townland]
      ,[Near]
      ,[Status]
      ,[Built]
     INTO #Results
      FROM [Houses]

      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
end

Houses.aspx

enter image description here

2 个答案:

答案 0 :(得分:0)

首先,我建议使用这样的NetTiers模型绑定到datasource  

然后你使用Eval作为这个   &#34;&GT; /&GT;

答案 1 :(得分:0)

更改存储过程的选择查询

SELECT ROW_NUMBER() OVER
      (
            ORDER BY [Name] ASC
      )AS RowNumber
      ,[Id]
      ,[Name]
      ,[Townland]
      ,[Near]
      ,[Status]
      ,[Built]
,[Image] 
     INTO #Results
      FROM [Houses]

并使用

 <img src='<%# Eval("Image") %>' alt="img" width="398" height="287"/>