2012-05-03 58 views
3

對ASP很新穎,我有什麼感覺像一個非常基本的問題。我在default.aspx.cs下面的代碼:爲什麼當我想加載另一個頁面時,Page_Load事件觸發?

 protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      //Get one day ago 
      DateTime oneDayAgo = DateTime.Now.ToLocalTime().AddDays(-1); 
      String strOneDayAgo = oneDayAgo.ToString(); 

      //Declare the query string 
      String queryString = "Select * from Computers Where whenCreated >= '" + strOneDayAgo + "' ORDER BY whenCreated DESC"; 

      //Show the query being used to the user 
      lblQueryUsed.Text = queryString; 

      // Run the query and bind the resulting DataSet to the GridView control. 
      DataSet ds = GetData(queryString); 
      if (ds.Tables.Count > 0) 
      { 
       GridView1.DataSource = ds; 
       GridView1.DataBind(); 
      } 
     } 
    } 

所有的作品非常漂亮,但是當用戶點擊頁面上的鏈接轉到所謂Reports.aspx另一個頁面的問題,同樣的Page_Load事件觸發,並且所有控件(lblQueryUsed,GridView1)由於某種原因被設置爲NULL,並且我得到一個異常。

爲什麼當我想要加載Reports.aspx時,default.aspx加載的Page_Load事件?爲什麼控件爲空?

非常感謝您的幫助。

編輯:這是該頁面的完整代碼,你還需要什麼?

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

namespace AD_watcher_web_app 
{ 
public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      //Get one day ago 
      DateTime oneDayAgo = DateTime.Now.ToLocalTime().AddDays(-1); 
      String strOneDayAgo = oneDayAgo.ToString(); 

      //Declare the query string 
      String queryString = "Select * from Computers Where whenCreated >= '" + strOneDayAgo + "' ORDER BY whenCreated DESC"; 

      //Show the query being used to the user 
      lblQueryUsed.Text = queryString; 

      // Run the query and bind the resulting DataSet to the GridView control. 
      DataSet ds = GetData(queryString); 
      if (ds.Tables.Count > 0) 
      { 
       GridView1.DataSource = ds; 
       GridView1.DataBind(); 
      } 
     } 
    } 

    protected void Label1_PreRender(object sender, EventArgs e) 
    { 
     //Populate the labels 
     lblCompCount.Text = GridView1.Rows.Count.ToString(); 
     lblTimeRun.Text = DateTime.Now.ToLocalTime().ToString(); 
    } 

    ///////////////////////METHODS/////////////////////// 

    DataSet GetData(String queryString) 
    { 
     // Set the connection string 
     SqlConnectionStringBuilder conBuilder = new SqlConnectionStringBuilder(); 
     conBuilder.DataSource = "dbsql01dev.llnl.gov"; 
     conBuilder.InitialCatalog = "XloadDB"; 
     conBuilder.IntegratedSecurity = true; 

     String connectionString = conBuilder.ConnectionString; 

     //Declare a new dataset 
     DataSet ds = new DataSet(); 

     try 
     { 
      // Connect to the database and run the query. 
      SqlConnection connection = new SqlConnection(connectionString);   
      SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 

      // Fill the DataSet. 
      adapter.Fill(ds); 
     } 
     catch(Exception ex) 
     { 
      // The connection failed. Display an error message. 
      lblExceptions.Text = ex.ToString(); 
      lblExceptions.Visible = true; 
     } 
     return ds; 
     } 
} 
} 
+0

鏈接是服務器端鏈接嗎? – Oded

+3

鏈接是在服務器上還是在客戶端上處理?換句話說,你是否有一個服務器上的點擊事件處理程序,你做了一個'Response.Redirect',或者是這些簡單的超鏈接? – zimdanen

+1

請粘貼aspx –

回答

8

對於服務器端控件工作,頁面需要控制事件觸發之前重新加載。

這是page lifecycle的一部分。

此行爲也會發生在服務器端的鏈接上 - 一旦發生回發,頁面將重新加載並觸發page_load

要避免這種情況,請將您的鏈接變爲純粹的客戶端鏈接。

因此,沒有runat="server",但正確的HTML <a href="">link</a>鏈接。

+0

恐怕OP沒有發佈完整的代碼,他正在做別的東西。即使他正在執行Response.Redirect並且不檢查IsPostback,也沒有理由將這些控件設置爲Page_Load上的null。 – Icarus

+0

所以這裏是構成我的Site.Master上的菜單欄的ASP:.Master: < asp:MenuItem NavigateUrl =「〜/ About.aspx」Text =「About」/> Dbloom

+0

@Icarus - 這仍然是服務器端控件 - ''。 @Dbloom - 這是webforms的工作原理。當您使用服務器端控件時,會發生回發並運行'page_load'。這就是爲什麼IsPostBack存在。 – Oded

相關問題