0

There is an exisiting page I am working on at where I work, and there is a checkbox in the ascx , and it has a CheckedChange event for it in its .cs The problem I am having with that page is that when i run the website and check the box, it never fires the event, instead it goes through the On_Load event and it never pass throgh the event... I am not sure why it is not firing the event...

 <asp:CheckBox runat="server" ID="OptionSelected" 
   OnCheckedChanged="OptionSelected_CheckedChanged"
  AutoPostBack="true" />&nbsp;
 <asp:Label runat="server" ID="OptionHeader"></asp:Label>

 protected void OptionSelected_CheckedChanged(object sender, EventArgs e)
    {
       //some code here
    }

PS: This is a control ascx sorry, I forgot to mention. Also another symptom is that the page where that control is... it refreshes unexpectedly, clears some data (including the checkbox if it is checked) and cant figure out what it is causing it.

Any ideas of the reason why this might be happening? things i should look for?

public PortalInfoPortal myPortal
    {
        get
        {
            return portal.GetPortalInfo();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //added this an nothing
        OptionSelected.CheckedChanged += new EventHandler(OptionSelected_CheckedChanged);


           if (myPortal != null)
        {
            lblOptionMsg.Text = "(" + myPortal.MaxOptionSelectedCharacters.ToString() + " char max)";

            txtMessage.MaxLength = myPortal.MaxOptionSelectedCharacters;
            var maxLength = myPortal.MaxOptionSelectedCharacters;
            maxCheck.ValidationExpression = @"^[\s\S]{0," + maxLength.ToString() + "}$";

        }
        //There is no binding of any type here
     }
user710502
  • 11,181
  • 29
  • 106
  • 161
  • Please note added new information under my question – user710502 Nov 11 '11 at 19:34
  • How are you loading the usercontrol? Are you binding any data on page load? Show this code including the events it is in if you can. – Valamas Nov 11 '11 at 19:35
  • possible duplicate of [OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked](http://stackoverflow.com/questions/1404528/oncheckedchanged-event-handler-of-aspcheckbox-does-not-fire-when-checkbox-is-un) – James Hill Nov 11 '11 at 19:46
  • no not really , that solution did not help me - i added what they mentioned there the OptionSelected.CheckedChanged += new EventHandler(OptionSelected_CheckedChanged); and nothing.. – user710502 Nov 11 '11 at 19:47

2 Answers2

3

you need to have viewstate turned on to have changed events fire. viewstate is how the server knows what the state of the control was when the page was originally served and before it was posted back. without this information, it doesn't know whether the value has changed. check to see if you have viewstate enabled.

Dave Rael
  • 1,759
  • 2
  • 16
  • 21
0

Are you perhaps creating the usercontrol (using Page.LoadControl()) and adding it to the control tree dynamically and then forgetting that those steps need to be performed on every postback? Otherwise, the usercontrol no longer exists on postback and none of its events will be fired.

wonkim00
  • 637
  • 3
  • 9
  • The page that uses the control, has a register tag on that page... And per my notes above, when debuggin that page.. it does hit the control because it goes through the Page_Load of the control.. what i dont understand is why the event does not fire instead of loading the whole control all the time and ignoring the event? – user710502 Nov 11 '11 at 19:57