Jon
Jon

Reputation: 3213

Button's Visible property changing inbetween Page_Init and Page_Load

Here are my buttons:

<asp:Button ID="PreviousButton" CssClass="previous-button button" Text="Previous" Visible="false" OnClick="PreviousButton_Click" runat="server" />
<asp:Button ID="NextButton" CssClass="next-button button" Text="Next" Visible="false" OnClick="NextButton_Click" runat="server" />

During Page_Init this code is executed:

NextButton.Visible = bNextEnabled;
PreviousButton.Visible = bPreviousEnabled;

Both bNextEnabled and bPreviousEnabled are True at this point.

At the end of Page_Init Response.Write(NextButton.Visible.ToString()) is True.

At the end of Page_Init Response.Write(PreviousButton.Visible.ToString()) is True.

--

At the start of Page_Load Response.Write(NextButton.Visible.ToString()) is False.

At the start of Page_Load Response.Write(PreviousButton.Visible.ToString()) is True.

I've extrapolated the code for the buttons but I'll provide anything else that might be pertinent, though I have no idea what that might be as yet.

At no other point on the page is the Visible property for either button altered. The page where the buttons stop working is a postback. The page prior to the postback both buttons are set to Visible = false though I can't imagine how this would effect things.

So what's happening here? Both buttons use exactly the same code and yet one works as expected and the other doesn't..

Upvotes: 1

Views: 1642

Answers (1)

patmortech
patmortech

Reputation: 10229

It's most likely related to the settings for the property getting saved into ViewState. That's the only thing between Init and Load that might have an effect on the control properties such as this (as far as I can tell). Init fires before the values from ViewState have been loaded and before ViewState tracking has been turned on, so it's possible you change it there and then it gets overwritten when the ViewState gets loaded.

If there's a reason you can't set these properties in the Load event instead of Init, you could try using the PreLoad event instead.

Here's a good reference for the page lifecycle and what's happening when.

Upvotes: 4

Related Questions