[英]Page_Load firing when following link to another page asp.net
I'm fairly new to ASP.NET, I've been reading a few questions related to this but I'm still unable to figure out what's wrong with my code, I have a default.aspx page with a menu on top created using a list (ul and li items) and putting the <a href="">
tag to create the links to other pages but after following a link to another page, the Page_Load event fires before leaving the page, I understand this would be the expected behavior with Response.Redirect, but I don't know how to avoid this using tags (if possible), this is the markup I'm using for the Default.aspx page:我是 ASP.NET 的新手,我一直在阅读一些与此相关的问题,但我仍然无法弄清楚我的代码有什么问题,我有一个 default.aspx 页面,顶部有一个菜单,使用创建一个列表(ul 和 li 项目)并放置
<a href="">
标签来创建到其他页面的链接,但是在链接到另一个页面之后,Page_Load 事件在离开页面之前触发,我知道这将是Response.Redirect 的预期行为,但我不知道如何使用标签来避免这种情况(如果可能),这是我用于 Default.aspx 页面的标记:
<ul id="lista">
<li><a href="Default.aspx"><strong>Inicio</strong></a></li>
<li><a href="Items.aspx"><strong>Item</strong></a></li>
<li><a href="IKs.aspx"><strong>IK</strong></a></li>
<li><a href="#"><strong>Acerca de</strong></a></li>
</ul>
And this is the code behind I have for Page_Load:这是我的 Page_Load 背后的代码:
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
ExcelUtility excel = new ExcelUtility();
dtDefault = excel.LeerExcel();
gridResults.DataSource = dtDefault;
gridResults.DataBind();
gridResults.VirtualItemCount = dtDefault.Rows.Count;
}
}
Basically, what I want to do is to follow the link to some other page without loading the default page before leaving, hope I make myself clear!基本上,我想做的是在离开之前不加载默认页面的情况下点击其他页面的链接,希望我说清楚!
Edit: The root cause of this was having the default <form runat="server">
tag at the beginning of the body section, this was causing the Page_Load event firing again in the same page once the links were being clicked, placing the hyperlinks outside of the form tag did the trick.编辑:其根本原因是在正文部分的开头有默认的
<form runat="server">
标记,这会导致在单击链接后在同一页面中再次触发 Page_Load 事件,放置超链接表单标签之外的技巧。
Your HTML code should be inside some HTML tag or custom ASP control which contains the attribute runat="server"
.您的 HTML 代码应该在一些包含属性
runat="server"
HTML 标记或自定义 ASP 控件内。 This is supposed to fire a PostBack
request to the server.这应该向服务器发出
PostBack
请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.