对 ASP 很陌生,我觉得这是一个非常基本的问题。我的 default.aspx.cs 文件中有以下代码:
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;
}
}
}
最佳答案
要使服务器端控件正常工作,页面需要在控件事件触发之前重新加载。
这是 page lifecycle 的一部分.
此行为也会发生在服务器端链接上 - 一旦发生回发,页面将重新加载并且 page_load
火灾。
为避免这种情况,请将您的链接设为纯客户端链接。
所以,没有 runat="server"
, 但正确的 HTML <a href="">link</a>
链接。
关于c# - 为什么要加载另一个页面时会触发 Page_Load 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437898/