atrljoe
atrljoe

Reputation: 8151

ASP.net Page Life Cycle Problem

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";

Upvotes: 0

Views: 2070

Answers (4)

Jeff Fritz
Jeff Fritz

Reputation: 9861

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.

Upvotes: 0

Joel C
Joel C

Reputation: 5567

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.

Upvotes: 3

Jack Marchetti
Jack Marchetti

Reputation: 15754

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

Upvotes: 0

Andre Pena
Andre Pena

Reputation: 59336

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

Upvotes: 1

Related Questions