Should I be able to modify other controls from the ASP.NET page event handler?

I am trying to disable a checkbox control from the CheckedChanged event handler of a checkbox. Should I do this?

At the moment, if I set Enabled to false, nothing changes on page reload. If I do the same in Page_Load, I see this change.

To clarify:

This does not 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
}

      

0


source to share


1 answer


Are you sure you are shooting in the order you expect? Put breakpoints on all your postback methods and see what happens, reload the enabled state anywhere? do you have enableviewstate = false on anything?

Edit: Do you realize that CheckedChanged doesn't fire until you exchange messages from another control, or do you have AutoPostBack = true on checbkbox?



This works great:

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

      

+2


source







All Articles