ASP.net页面生命周期问题

发布于 2024-11-01 06:33:39 字数 619 浏览 1 评论 0原文

我了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伏妖词 2024-11-08 06:33:39

您始终可以直接检查后变量。如果您的提交按钮被单击,它将在 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.

不如归去 2024-11-08 06:33:39

有多种不同的方法可以解决这个问题。

  • Page_Load 代码放在 OnPreRender 上。这将在 RaisePostBackEvent 之后发生,因此可以保证任何按钮的点击都已被处理。重要的是要注意,根据您要在那里做什么,它可能不起作用。某些组件不接受此类后续事件中的某些属性更改。如果可行的话,这是推荐的方法。
  • 在按钮上创建一个新事件,当您更改 ImageButton 的 CommandArgument 时会触发该事件。然后,在 Page_Load 上,为新按钮的事件创建一个处理程序,并将示例中位于 Page_Load 中的代码放在那里。使用此方法将导致此处理在 OnPreRender 之前发生

There's a variety of different ways to solve this problem.

  • Place your Page_Load code on OnPreRender. This will happen after RaisePostBackEvent 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.
  • Create a new event on your button that is triggered when you change the ImageButton's CommandArgument. Then, on Page_Load, create a handler for your new button's event and put there the code that in your example is in the Page_Load. Using this approach will cause this processing to happen prior to OnPreRender
不如归去 2024-11-08 06:33:39

不过,我想这取决于您的 Page_Load 事件中发生的其他情况。

如果在 page_load 中设置该变量,则可以将其包装在:

If(!Page.IsPostback) {
   // only execute when the page first loads
}

并且只需为 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:

If(!Page.IsPostback) {
   // only execute when the page first loads
}

And just have an OnClick event for your ImageButton

自此以后,行同陌路 2024-11-08 06:33:39

在 Asp.Net Webforms 配置中,ImageButton 单击应触发 ImageButton_OnClick 类型的事件。无论是在标记中还是在 Page_Init 事件方法中,您都需要连接此事件处理程序:

在标记中:

<asp:imagebutton click="ImageButton_Onclick" runat="server" id="displayYImgBtn" />

在代码隐藏中:

public void Page_Init() {
   displayYImgBtn.Click += ImageButton_OnClick;
}

在任一模型中,都应该创建按钮,并且应该在回发时执行您的方法(当按钮被点击)。

如果您希望在触发按钮单击事件之前设置其他配置信息,则需要在 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:

<asp:imagebutton click="ImageButton_Onclick" runat="server" id="displayYImgBtn" />

In the code-behind:

public void Page_Init() {
   displayYImgBtn.Click += ImageButton_OnClick;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文