對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 –