2

I have an asp.net label control with its Visible property set to false. In the code-behind I'm setting its visibility to true/false based on whether the user has logged in or not. A weird behavior is that on postbacks, the code-behind executes and sets the value of the label's visibility to whatever, and then the markup executes and sets the label's visibility to false. On non-postback requests, the status set by the code-behind is not overwritten. Pardon my ignorance, but am I missing something important?

5
  • can you please post your code to clarify?
    – TJB
    Feb 25, 2009 at 4:56
  • it sounds like you are coding to the wrong events and stepping on the page lifecycle. post code so we can have a look.
    – Jason
    Feb 25, 2009 at 4:59
  • I've taken care of checking IsPostback on PageLoad. Code is insignificant here. My question is- Why does the markup execute after the code-behind? Is there something fundamental I am missing? Feb 25, 2009 at 5:06
  • Did you do a trace and see what happens? or when it breaks?
    – Shoban
    Feb 25, 2009 at 5:07
  • -1: Why have you ticked an answer that is completely unrelated to how the problem was solved?
    – Alex Angas
    Nov 29, 2010 at 2:58

3 Answers 3

5

Get to know the page lifecycle, and when the various page events are fired by the runtime.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Also, I don't think the code is insignificant. If it were coded properly, you wouldn't be experiencing this problem. Put it up here and let us have a look.

2

There's no code posted so you might already have this.

are you wrapping everything in your Page_Load method with

if(!Page.IsPostback)
{
    // your code here.
}

?

1
  • Yeah, I've taken care of checking IsPostback. Feb 25, 2009 at 5:05
1

does your Label has a EnableViewState="false" attribute?

Learn more on this attribute at http://www.w3schools.com/ASPNET/aspnet_viewstate.asp

1
  • It was just one of those queer things a developer faces occasionally. Weirdly, a machine restart fixed it. Would you believe it?! Thanks for your inputs anyway. Mar 4, 2009 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.