2012-05-03 60 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

相关问题