2011-09-14 1 views
0

EDIT:コードを更新しました。残りの問題は、「スレッドがコード0で終了しました」というエラーが発生するのを待つ必要があることです。新しい検索。私はボタンイベントが発生しないが、page_Loadは発生しない。それを処理する方法はありますか?queryStringでリストを検索しているときにスレッドに問題があります(リダイレクト)

私はaspxウェブページ(localhost)を作成して認定資格を取得しています。このページには、データを表示するgrindview、検索ボックス、およびサマリー検索のボタンが含まれています。私はLinqを使用してデータベースから結果を取得します。クエリーストリングはポストバック中に検索を保存し、キャッシュは検索されない結果を保存します。ページの読み込みが遅くなり、リフレッシュ後に読み込みが高速になります(キャッシングがおそらく動作するようになります)。また、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("---------------"); 
     } 

答えて

2

あなたはマッチオブジェクトのリストは、ブレークポイントを使用せずに、DB対キャッシュから取得されているかどうかを確認するためにそこにいくつかのデバッグコードを入れてみましたがありますか?

----更新----私は(データベースのビットを除く)あなたのコードを取られ、ASP.NET 4.0 Webアプリケーションにそれを入れて、すべてであるように思わまし

それのためにコードビハインドここ

<%@ 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> 

とされています:ここで

はaspxページです...細かい作業

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に問題がありますか?

+0

私が追加した場合、デバッグコードを使用中にいくつかのものが見つかりました。 Debug.WriteLine( "Button Pressed"); Button_Searchの内部でプログラムは決して出力にそれを書きませんでした。次に、検索が成功するたびに、mscorlib.dllで 'System.Threading.ThreadAbortException'という型の最初の例外が発生しました mscorlib.dllで 'System.Threading.ThreadAbortException'型の例外が発生しましたが、ユーザーで処理されませんでしたコードを出力 – Olle89

+0

残っている問題は、私が新しい検索を行う前に、「スレッドがコード0で終了しました」というメッセージが出るのを待つ必要があるということです。私はボタンイベントが発生しないが、page_Loadは発生しない。 – Olle89

+0

使用している.NETのバージョンは何ですか?私はちょうどサンプルのASP.NET 4.0を作成して、あなたのコードはうまく動作するようです - どこでPage_Loadハンドラの '検索'と 'リスト'変数を宣言していますか? – codefrenzy

関連する問題