简体   繁体   English

当我想加载另一个页面时,为什么会激活Page_Load事件?

[英]Why is the Page_Load event firing when I want to load another page?

Very new to ASP, and I have what feels like a very basic question. 对ASP来说很新,我觉得这是一个非常基本的问题。 I have the following code in my default.aspx.cs file: 我在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();
            }
        }
    }

All of that works very nicely, but the problem is when a user clicks a link on that page to go to another page called Reports.aspx, that same Page_Load event fires, and all of the controls (lblQueryUsed, GridView1) are set to NULL for some reason, and I get an exception. 所有这些都非常好用,但问题是当用户单击该页面上的链接转到另一个名为Reports.aspx的页面时,同样的Page_Load事件会触发,并且所有控件(lblQueryUsed,GridView1)都设置为NULL由于某种原因,我得到一个例外。

Why is the Page_Load event for default.aspx loading when I want to load Reports.aspx? 当我想加载Reports.aspx时,为什么要加载default.aspx的Page_Load事件? Why are the controls null? 为什么控件为空?

Thanks a lot for the help. 非常感谢您的帮助。

EDIT:Here is the full code of that page, what else do you need? 编辑:这是该页面的完整代码,您还需要什么?

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;
      }
}
}

For server side controls to work, the page needs to reload before the control events fire. 要使服务器端控件起作用,页面需要在控制事件触发之前重新加载。

This is part of the page lifecycle . 这是页面生命周期的一部分。

This behaviour will also occur on server side links - once a postback occurs, the page reloads and page_load fires. 此行为也将发生在服务器端链接 - 一旦发生回发,页面重新加载和page_load触发。

To avoid this, make your links into pure client side links. 要避免这种情况,请将链接转换为纯客户端链接。

So, no runat="server" , but proper HTML <a href="">link</a> links. 所以,没有runat="server" ,但正确的HTML <a href="">link</a>链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM