views:

46

answers:

3

Hi, I'm trying to disable a label control from the CheckedChanged event handler of a checkbox. Should I be able to do this?

At the moment if I set Enabled to false nothing changes when the page reloads. If I do the same thing in Page_Load then I see the change.

To clarify:

This doesn't work:

protected void chkNeverExpires_CheckedChanged(object sender, EventArgs e)
{
    this.lblMessage.Enabled = false
}

But this does:

protected void Page_Load(object sender, System.EventArgs e)
{
    this.lblMessage.Enabled = false
}
+2  A: 

Are you sure you're events are firing in the order you expect? Put breakpoints on all your postback methods and watch what happens, are you resetting the enabled state anywhere? do you have enableviewstate=false on anything?

Edit: You realise CheckedChanged doesnt fire until you postback from another control, or you have AutoPostBack=true on the checbkbox?

This works fine:

<asp:Label runat="server" ID="lblTest">test</asp:Label>
<asp:CheckBox runat="server" ID="chkCheck" AutoPostBack="true" />Check

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    chkCheck.CheckedChanged += chkCheck_CheckedChanged;
}

private void chkCheck_CheckedChanged(object sender, EventArgs e)
{
    lblTest.Enabled = false;
}
Andrew Bullock
Yeah, I do have AutoPostBack set to true. I have no extra lines in my OnInit event handler, which is interesting. I wonder if that is related?
James
My event DOES fire though, I just can't seem to alter the rest of the page from it though. The changes don't persist anywhere.
James
A: 

Trull, are you saying it should work?!

When I click the checkbox I see the following events:

Page_Load CheckedChanged

I have no other events to watch.

I'm not resetting the enabled state. It's not running through much code in the Page_Load event so that's easy for me to check.

I don't have enableviewstate=false anywhere in my page.

James
A: 

I think that my page is just screwed up to be honest.

It appears to be some sort of dirty ASP.NET 1.1 -> 2.0 conversion.

I'm gonna mark your reply as my answer, because you have told me that it should work!

James
Thanks :) the main problem i had when learning how to do this stuff was the order in which events fire. Put breakpoints everywhere and have a play around, see what happens in what order. There are plenty of explainations on google, but theres no subsitute for getting your hands dirty ;)
Andrew Bullock
Yeah, true. I'm pretty certain I have the event order ok. There's something else very dodgy about this page. If I manually add the event hookup to the OnInit handler it calls the CheckedChanged event twice, so hell knows where the other hookup is?!
James