【问题标题】:Why is the Page_Load event firing when I want to load another page?当我想加载另一个页面时,为什么会触发 Page_Load 事件?
【发布时间】:2012-05-03 19:14:35
【问题描述】:

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

【问题讨论】:

  • 链接是服务器端链接吗?
  • 链接是在服务端处理还是在客户端处理?换句话说,您是否在服务器上为他们的点击事件提供了处理程序,并且您执行了Response.Redirect,或者这些简单的超链接?
  • 请粘贴aspx的相关部分
  • 如果链接导致回帖,那么您的页面将再次加载。
  • 我的链接看起来像这样,并且在 Site.Master 上:

标签: c# asp.net events pageload


【解决方案1】:

要使服务器端控件正常工作,页面需要在控件事件触发之前重新加载。

这是page lifecycle 的一部分。

这种行为也会发生在服务器端链接上——一旦发生回发,页面会重新加载并触发page_load

为避免这种情况,请将您的链接设为纯客户端链接。

所以,没有 runat="server",而是正确的 HTML <a href="">link</a> 链接。

【讨论】:

  • 恐怕 OP 没有发布完整的代码,他正在做其他事情。没有理由在 Page_Load 上将这些控件设置为 null,即使他正在执行 Response.Redirect 并且没有检查 IsPostback。
  • 这是构成我的 Site.Master 上的菜单栏的 ASP:
  • @Icarus - 这仍然是一个服务器端控件 - <asp:Menu>。 @Dbloom - 这就是网络表单的工作方式。当您使用服务器端控件时,会发生回发并运行page_load。这就是IsPostBack 存在的原因。
  • 但是我有 if (!IsPostBack) 的声明,并且 Postback 显示为 False,所以这是否意味着没有发生 postback?抱歉,页面生命周期对我来说仍然很模糊。
  • 我一直在尝试制作链接客户端,但 行位于我的 Site.Master 的顶部,这是否意味着所有内容都包含在内 标签中的将是服务器端吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多