C# 当我想加载另一个页面时,为什么会触发Page_Load事件?
对ASP非常陌生,我有一个非常基本的问题。我的default.aspx.cs文件中有以下代码:C# 当我想加载另一个页面时,为什么会触发Page_Load事件?,c#,asp.net,events,pageload,C#,Asp.net,Events,Pageload,对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
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的页面时,会触发同一个页面加载事件,并且由于某种原因,所有控件(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\u load
要避免这种情况,请将链接转换为纯客户端链接
因此,没有
runat=“server”
,只有正确的HTML
链接。链接是服务器端链接吗?链接是在服务器上还是在客户端处理的?换句话说,您是否在服务器上为他们的点击事件设置了处理程序,并执行了响应。重定向,或者这些是简单的超链接?请粘贴相关的aspxIf链接片段,然后重新加载页面。我的链接如下所示,大师:恐怕OP没有发布完整的代码,他在做别的事情。没有理由在页面加载时将这些控件设置为null,即使他正在执行响应。重定向,而不是检查iPostBack。因此,这里是组成我的网站菜单栏的ASP。Master:@Icarus-这是仍然是服务器端控件-
@Dbloom-这就是webforms的工作方式。使用服务器端控件时,会发生回发并运行page\u load
。这就是为什么IsPostBack
存在的原因。但是我有一条语句if(!IsPostBack),并且Postback显示为False,那么这不意味着没有发生Postback吗?很抱歉,页面生命周期对我来说仍然很模糊。我一直在尝试在客户端创建链接,但该行位于我的站点顶部。Master,那么这是否意味着标签中包含的所有内容都将是服务器端的?