asp.net OnPreLoad 不触发?
我不明白为什么我的 OnPreLoad 函数没有被 search.aspx.cs 触发。
*更新* - 好的,我找出了导致问题的原因,但我不明白为什么会出现问题。我更新了我的问题。
以下是三个相关文件:
// 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 确实被触发了。这是怎么回事?为什么我的 OnPreLoad 不会触发?
** 更新 ** - 在我的 搜索中将
。有人向我解释为什么这解决了问题吗?我为此苦苦挣扎了几个小时。 后,我的 onpreload 被触发.aspx
I can't figure out why my OnPreLoad function doesn't get fired for search.aspx.cs.
*Update * - OK, I figured out what's causing the problem, but I don't understand why it is a problem. I updated my question.
Here are three relevant files:
// 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();
}
}
What's weird is that the OnUnload DOES get fired. What's going on? Why won't my OnPreLoad fire?
** UPDATE ** - I got my onpreload to fire after I changed <select runat="server">
to <select>
in my search.aspx
. Someone explain to me why this fixed the problem? I was struggling for hours with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
select
的方式可能会有些奇怪:您不应该在该标记内嵌套
Repeater
。实际上,我很惊讶您没有因此而出现编译/运行时错误。您注意到您找到了可以使用的
DropDownList
控件。但是,HttpSelectList
控件也有一个类似的界面,用于向其中添加项目 - 您不需要在其中添加额外的列表控件。我的猜测是,由于删除它作为服务器控件修复了问题(修复了语法怪异),因此存在一些微妙的情况阻止了正常的 OnPreLoad 在该语法中被触发。
There might be something strange with how you are using your
select
:You should not be nesting a
Repeater
inside that tag. I'm actually surprised you don't get a compilation/runtime error due to that.You note that you found the
DropDownList
control to use instead. However, theHttpSelectList
control also has a similar interface for adding items to it - you don't need an extra list control inside it.My guess is, since removing that as a server control fixed the problem (which fixes the syntax weirdness), that there is something subtle going on which prevents the normal OnPreLoad from being fired in that syntax.
在 BTPage 中,您不会调用 base.OnPreLoad:
不过,我不知道为什么这会阻止搜索类覆盖该方法,也不知道
select
与什么有关。 (例如,无论如何它应该有效)。但通常来说,在任何重写方法中调用基本方法是一种很好的做法(甚至是专门供您重写的方法)。您不知道核心Page.OnPreLoad
方法中是否会发生任何其他应该发生的事情。In BTPage you don't call base.OnPreLoad:
I don't know why this would prevent the search class overriding the method, though, or what the
select
has to do with anything. (e.g. it should have worked anyway). But it is generally good practice to call the base method in any overridden method (even ones like this that are there specifically for you to override). You don't know if anything else that is supposed to take place happens in the corePage.OnPreLoad
method.