ASP.net页面生命周期问题
我了解 ASP.net 页面生命周期,我知道当我单击按钮时,它首先重新加载页面,然后执行按钮后面的代码。我的问题是如何获取它,以便我可以在此过程发生之前进行设置。我试图传递一个已单击按钮的变量,但由于页面生命周期,它永远不会收到此变量。我该怎么做才能确定这个按钮已被点击也许我想太多了,甚至想得太少了。
页面加载:
ImageButton btn = (ImageButton)(Page.FindControl("displayYImgBtn"));
if (btn != null)
{
string state = btn.CommandArgument;
if (state == "true")
{
search();
btn.CommandArgument = "false";
}
}
按钮:
ImageButton btn = (ImageButton)(sender);
btn.CommandArgument = "true";
I understand the ASP.net Page Life Cycle, I get that when I click a button, it first reloads the page then it executes the code behind for my button. My question is how do I get it so that I can set something before this process happens. I am trying to pass a variable that the button has been clicked, but because of the page life cycle it never receives this. What can I do so that I can determine that this button has been clicked maybe I am over-thinking this, or even under-thinking.
Page Load:
ImageButton btn = (ImageButton)(Page.FindControl("displayYImgBtn"));
if (btn != null)
{
string state = btn.CommandArgument;
if (state == "true")
{
search();
btn.CommandArgument = "false";
}
}
Button:
ImageButton btn = (ImageButton)(sender);
btn.CommandArgument = "true";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您始终可以直接检查后变量。如果您的提交按钮被单击,它将在
Request.Form
集合中拥有一个值,如果它不是回发或某些其他控件导致回发,则不应出现。You could always inspect the post variables directly. If your submit button was clicked, it will have a value in the
Request.Form
collection, if it's not postback or some other control caused postback, it shouldn't appear.有多种不同的方法可以解决这个问题。
Page_Load
代码放在OnPreRender
上。这将在RaisePostBackEvent
之后发生,因此可以保证任何按钮的点击都已被处理。重要的是要注意,根据您要在那里做什么,它可能不起作用。某些组件不接受此类后续事件中的某些属性更改。如果可行的话,这是推荐的方法。There's a variety of different ways to solve this problem.
Page_Load
code onOnPreRender
. This will happen afterRaisePostBackEvent
so it is guaranteed that any button's click have already been processed. It's important to notice that, depending on what you're going to do there, it may not work. Some components do not accept some property changes in such a later event. This is the recommended approach if it works.不过,我想这取决于您的 Page_Load 事件中发生的其他情况。
如果在 page_load 中设置该变量,则可以将其包装在:
并且只需为 ImageButton 提供一个 OnClick 事件
I suppose it depends on what else is happening in your Page_Load event, however.
If that variable is being set in the page_load, you can wrap it in:
And just have an OnClick event for your ImageButton
在 Asp.Net Webforms 配置中,ImageButton 单击应触发 ImageButton_OnClick 类型的事件。无论是在标记中还是在 Page_Init 事件方法中,您都需要连接此事件处理程序:
在标记中:
在代码隐藏中:
在任一模型中,都应该创建按钮,并且应该在回发时执行您的方法(当按钮被点击)。
如果您希望在触发按钮单击事件之前设置其他配置信息,则需要在 Page.Load 事件或 Page.Init 事件期间执行此操作。无论您何时选择访问控件并设置变量,都不得以任何方式更改它被发回的状态。如果 ImageButton 的格式与之前的格式不完全一样,您将不会收到回发事件。
In an Asp.Net webforms configuration, the ImageButton click should trigger an ImageButton_OnClick type of event. Either in your markup or in the Page_Init event method, you will need to connect this event handler:
In markup:
In the code-behind:
In either model, you button should be created and your method should be executed on postback (when the button is clicked).
If you wish to set other configuration information before the button click event is triggered, you will need to do that during either the Page.Load event, or the Page.Init event. No matter WHEN you choose to reach out to your control and set variables, you must NOT change it in any way from the state at which it was posted back. If the ImageButton is not formatted EXACTLY as it was when it was previously, you will not receive the postback event.