gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:47 26 4
gpt4 key购买 nike

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

数据库结构

[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

最佳答案

更改存储过程的选择查询

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"/>

关于c# - 在 ASP.NET Web 窗体网站上存储和显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31243668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com