我正在创建一个aspx网页(localhost)来研究认证。该页面包含一个grindview,用于显示数据,搜索框和峰值搜索按钮。我使用Linq从数据库中获取结果,使用查询字符串在回发时存储搜索,使用Cache来存储未搜索的结果。第一次页面加载速度慢,刷新后加载速度很快(因此缓存可能有效)。 Linq查询还提供了预期结果,并且站点上的URL与文本框中的用户类型一致。
Public void Button_Search(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Button Pressed");
String s = ("~/Matches.aspx");
if (TextBox1.Text != null && TextBox1.Text != "")
{
s = (s + "?Search=" +TextBox1.Text);
}
Debug.WriteLine("Redirction adress:" +s);
Response.Redirect(s, false);
Context.ApplicationInstance.CompleteRequest();
}
然后我的page_Load函数
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Debug.WriteLine("---------------");
Debug.WriteLine("PageLoad");
if (Request.QueryString["Search"] != null)
{
Debug.WriteLine("SerchValueFound");
search = Request.QueryString["Search"];
}
if (Cache["MatchesCache"] == null)
{
Debug.WriteLine("Cache Loading");
using (ConnectionToDBDataContext context = new ConnectionToDBDataContext())
{
try
{
var lista = (from game in context.Games
join home in context.Teams on game.HomeTeamID equals home.TeamID
join away in context.Teams on game.AwayTeamID equals away.TeamID
join arenaName in context.Arenas on game.ArenaID equals arenaName.ArenaID
select new Match
{
MatchID = (int)game.MatchID,
Date = (int)game.Date,
TimeStart = game.TimeStart,
HomeTeam = home.TeamName,
AwayTeam = away.TeamName,
HomeGoals = (int)game.HomeTeamGoals,
AwayGoals = (int)game.AwayTeamGoals,
Arena = arenaName.ArenaName,
Line = "-"
});
list = lista.ToList();
Cache.Insert("MatchesCache", list, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);
}
catch { Debug.WriteLine("Failed to update cache"); }
}
}
list = (List<Match>)Cache["MatchesCache"];
Debug.WriteLine("List loaded from Cache");
if (search != null && search != "")
{
Debug.WriteLine("Search is beeing done");
List<Match> newList = new List<Match>();
foreach (Match m in list)
{
if (m.AwayTeam.Contains(search) || m.HomeTeam.Contains(search))
{
newList.Add(m);
}
}
list = newList;
}
GridView1.DataSource = list;
GridView1.DataBind();
search = "";
Debug.WriteLine("---------------");
}
答案 0 :(得分:2)
您是否尝试过在其中放置一些调试代码来检查是否正在从Cache与DB中检索Match对象列表,而不使用断点?
----更新----
我已经使用了你的代码(数据库位除外)并将其放入ASP.NET 4.0 Web应用程序中,这一切似乎都运行良好......
这是aspx页面:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
以下是它的代码隐藏:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Match> list;
string search = null;
Debug.WriteLine("---------------");
Debug.WriteLine("PageLoad");
if (Request.QueryString["Search"] != null)
{
Debug.WriteLine("SerchValueFound");
search = Request.QueryString["Search"];
}
if (Cache["MatchesCache"] == null)
{
Debug.WriteLine("Cache Loading");
try
{
list = new List<Match>
{
new Match{MatchId = 1, Date = 1, TimeStart = 1, AwayTeam = "the flying fijians", HomeTeam = "wallabies"},
new Match{MatchId = 2, Date = 1, TimeStart = 1, AwayTeam = "wallabies", HomeTeam = "all blacks"},
new Match{MatchId = 3, Date = 1, TimeStart = 1, AwayTeam = "springboks", HomeTeam = "all blacks"},
};
Cache.Insert("MatchesCache", list, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);
}
catch
{
Debug.WriteLine("Failed to update cache");
}
}
list = (List<Match>)Cache["MatchesCache"];
Debug.WriteLine("List loaded from Cache");
if (!string.IsNullOrEmpty(search))
{
Debug.WriteLine("Search is beeing done");
var newList = new List<Match>();
foreach (var m in list)
{
if (m.AwayTeam.Contains(search) || m.HomeTeam.Contains(search))
{
newList.Add(m);
}
}
list = newList;
}
GridView1.DataSource = list;
GridView1.DataBind();
Debug.WriteLine("---------------");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Button Pressed");
var s = ("~/Default.aspx");
if (!string.IsNullOrEmpty(TextBox1.Text))
{
s = (s + "?Search=" + TextBox1.Text);
}
Debug.WriteLine("Redirction adress:" + s);
Response.Redirect(s, false);
}
}
public class Match
{
public int MatchId { get; set; }
public int Date { get; set; }
public int TimeStart { get; set; }
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
}
}
您的ConnectionToDBDataContext可能存在问题吗?