C# 在 Page_Load 之前调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1364346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function before Page_Load
提问by marcgg
I have a button that calls function A()
我有一个调用函数 A() 的按钮
When I click on it I want the calls to be made in that order:
当我点击它时,我希望按以下顺序进行调用:
A()
Page_Load()
Right now it's doing:
现在它正在做:
Page_Load()
A()
Is there a way around that or is it just by design and there's nothing I can do about it?
有没有办法解决这个问题,或者只是设计而我无能为力?
采纳答案by Anderson Imes
The easiest way to do this would be to use a HTML Submit button and check to see if it is in the Form on every postback in Page_Init
最简单的方法是使用 HTML 提交按钮并检查它是否在 Page_Init 中每次回发的表单中
public void Page_Init(object o, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.Form["MyButtonName"]))
{
A();
}
}
And in your ASP.NET code:
在你的 ASP.NET 代码中:
<Button Type="Submit" Name="MyButtonName" Value="Press Here To Do Stuff Early!" />
I think that will work.
我认为这会奏效。
回答by Joseph
You need to call your function in the Page_Init. Page_Init will happen before Page_Load.
您需要在 Page_Init 中调用您的函数。Page_Init 将在 Page_Load 之前发生。
Here's an Overview of the ASP.NET Page Lifecycle.
这是ASP.NET 页面生命周期的概述。
回答by Jeff Sternal
Not exactly: ASP.NET will always call Page_Load
before handling postback events like Button_Click
.
不完全是:ASP.NET 将始终Page_Load
在处理回发事件(如Button_Click
.
However, you can accomplish what you want by redirecting to your page after handling the postback event. (Using the Post-Redirect-Get pattern.)
但是,您可以通过在处理回发事件后重定向到您的页面来完成您想要的操作。(使用Post-Redirect-Get 模式。)
Inside your Page_Load method, you can avoid running any relevant code twice by checking to see if it's a postback first:
在 Page_Load 方法中,您可以通过先检查是否是回发来避免运行任何相关代码两次:
if (!this.IsPostBack) {
// Do something resource-intensive that you only want to do on GETs
}
回答by Mr. Smith
I don't think it's possible, at least, not in the way described by your question. When you click a button it will send a request to the server which in turn will start processing it, and follow the ASP.NET Page Lifecycle as posted by Joseph.
我不认为这是可能的,至少,不是你的问题所描述的方式。当您单击一个按钮时,它会向服务器发送一个请求,服务器将开始处理它,并遵循 Joseph 发布的 ASP.NET 页面生命周期。
Alternatively you could try making an AJAX call to a page without reloading the current one you're on and do whatever processing you require.
或者,您可以尝试对页面进行 AJAX 调用,而无需重新加载当前所在的页面,并执行您需要的任何处理。
回答by Rune FS
Control events (such as the click events of buttons) are called after page_load. The controls are not guarenteed to be fully initialized prior to page_load. If you really need to call a function before page_load has been called based on whether a button has been pressed you'll have to examine the request to check if the button has been pressed (basically old school ASP)
在page_load之后调用控制事件(如按钮的点击事件)。不保证在 page_load 之前完全初始化控件。如果您确实需要在根据按钮是否被按下而在 page_load 被调用之前调用一个函数,您将必须检查请求以检查按钮是否已被按下(基本上是老派的 ASP)
回答by Stephen
As Jeff Sternal answered, The Post-Redirect-Get pattern is a good way of solving a problem like this.
正如 Jeff Sternal 回答的那样,Post-Redirect-Get 模式是解决此类问题的好方法。
In my circumstances i had a calendar and if you clicked a date it would add that to a scheduler. The scheduler would have buttons on each new date that needed to have onclick functions tied to them.
在我的情况下,我有一个日历,如果您单击一个日期,它会将其添加到调度程序中。调度程序将在每个新日期都有按钮,需要将 onclick 功能绑定到它们。
Because the new row was being added with a linkbutton(on the calendar), in the code the new scheduler date was being added at the Postback event handling meaning that the new set of buttons wouldn't have a command tied to them.
因为新行是用链接按钮(在日历上)添加的,所以在代码中,新的调度程序日期是在回发事件处理中添加的,这意味着新的按钮集不会有与之相关的命令。
回答by sunnysidedown916
This is what you want to do for Page Init is called before Page Load.
Take a look at the ASP.net Page Life Cycle
这就是你要为 Page Init 在 Page Load 之前调用做的事情。
看一看 ASP.net 页面生命周期
public void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//CALL YOU FUNCTION A()
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
回答by Jon Schneider
If your actual goal here is to have your "page loading code" happen after your event handler runs -- for example, if clicking your button changes something in your database, and you want the updated data to be reflected on the page when it loads -- then you could have your "page loading code" get called from a method that gets called laterin the ASP.NET page life cyclethan your event handler, such as Page_PreRender
, instead of calling it from Page_Load
.
如果您的实际目标是在事件处理程序运行后让您的“页面加载代码”发生——例如,如果单击按钮更改了数据库中的某些内容,并且您希望更新的数据在加载时反映在页面上-- 然后,您可以从在ASP.NET 页面生命周期中比事件处理程序更晚调用的方法调用“页面加载代码” ,例如,而不是从.Page_PreRender
Page_Load
For example, here's a simplified excerpt from an .aspx.cs page class that has a button event handler that runs before the page population logic, and a confirmation message that is visible on the page only after the button was clicked:
例如,以下是 .aspx.cs 页面类的简化摘录,该页面类具有在页面填充逻辑之前运行的按钮事件处理程序,以及仅在单击按钮后在页面上可见的确认消息:
// Runs *before* the button event handler
protected void Page_Load() {
_myConfirmationMessage.Visible = false;
}
// Runs *after* the button event handler
protected void Page_PreRender() {
// (...Code to populate page controls with database data goes here...)
}
// Event handler for an asp:Button on the page
protected void myButton_Click(object sender, EventArgs e) {
// (...Code to update database data goes here...)
_myConfirmationMessage.Visible = true;
}