我不明白爲什麼我的OnPreLoad函數不會被search.aspx.cs解僱。
* 更新* - 好的,我找出了導致問題的原因,但我不明白爲什麼它是一個問題。我更新了我的問題。asp.net OnPreLoad不會觸發?
這裏有三個相關文件:
// search.aspx -- THIS <select runat="server"> CAUSED ALL the problem
<select runat="server" id="slctCategories">
<asp:Repeater runat="server" ID="optCategories">
<ItemTemplate>
<option value=""></option>
</ItemTemplate>
</asp:Repeater>
</select>
// search.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class search : BTPage
{
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
// BTPage.aspx.cs - the file that search.aspx.cs inherits from
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class BTPage : System.Web.UI.Page
{
protected SqlConnection cnx;
protected override void OnPreLoad(EventArgs e)
{
cnx = new SqlConnection(ConfigurationManager.AppSettings["database"]);
cnx.Open();
}
protected override void OnUnload(EventArgs e)
{
cnx.Close();
}
}
什麼奇怪的是,onunload的DOES會被解僱。這是怎麼回事?爲什麼我的OnPreLoad不會啓動?
**更新** - 我得到了我的onpreload火我在search.aspx
改變<select runat="server">
到<select>
後。有人向我解釋爲什麼這解決了這個問題?我一直在爲此掙扎數小時。
這很奇怪......你有沒有理由不使用'DropDownList'? – Town 2011-04-11 22:22:20
是的,我剛剛偶然發現了asp:dropdownlist。我只用.net玩了大約1天 – John 2011-04-11 22:26:17