对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;
}
}
}
链接是服务器端链接吗? – Oded
链接是在服务器上还是在客户端上处理?换句话说,你是否有一个服务器上的点击事件处理程序,你做了一个'Response.Redirect',或者是这些简单的超链接? – zimdanen
请粘贴aspx –