2

I have an ASP.NET checkbox that based on certian logic I need to set its checked value to true server-side. This is easy enough, but it fires the wired up MyCheckBox_CheckedChanged(object sender, EventArgs e) event as well.

That event's code is 100% ok, as long as it is called when the user checks the check box from the client. However, when I'm just trying to set that checkbox to be Checked = true; server-side I don't want the code in the event to run.

The cleanest way I would like to approach this was to inspect the sender object on the event and see if this event was really called by the client or not (like: http://msdn.microsoft.com/en-us/library/aa457091.aspx). However this did not yield any distinguishing information. It still looked like the checkbox being selected called it.

My 2nd thought is to set some session value and inspect this to basically return if not being called by the user. I can easily do this, but I don't like using 'public flags' of sorts unless absolutely necessary. I'd rather (if at all possible) inspect the sender or arguments on that event to determine if that property was set server side on reload of data from the database, or if the user actually selected it.

I saw this Prevent postback on server-side property change and it was not a direct solution to my question, but rather a work around not applicable to my situation.

Is there a way to determine this was set solely server side so that I can bypass running this code?

Community
  • 1
  • 1
atconway
  • 20,624
  • 30
  • 159
  • 229
  • 1
    can you provide a working example? i'm trying to reproduce what do describe and i can't seem to trigger the event server side. – Fredou Sep 30 '13 at 17:38
  • I agree that a session value is a bit messy, but perhaps a private value in the page's class as a compromise? It may still feel like a messy flag, but at least it's fully encapsulated within the object. – David Sep 30 '13 at 17:38
  • Perhaps this will help: http://stackoverflow.com/questions/3457136/asp-net-checkbox-does-not-fire-checkedchanged-event-when-unchecking – MikeSmithDev Sep 30 '13 at 17:44
  • Can you post some code sample? How the checkbox is declared, how the event is attached? Can't make this to happen – Yuriy Galanter Sep 30 '13 at 20:11
  • I agree with Yuriy and Fredou. I've never had the `CheckedChanged` event fire in response to programmatically changing the `.Checked` value. It would be good to see your code. – Josh Darnell Sep 30 '13 at 20:15

2 Answers2

1

Add a private boolean variable dontFire to the top of your class with a default value = false.

Then use the following:

dontFire = True;
myCheckbox.Checked = True;
dontFire = False;

Then in the myCheckBox.CheckedChanged event put this at the top:

if (dontFire) return;

Problem solved.

mindofsound
  • 78
  • 1
  • 1
  • 6
  • Yeah I suppose this could work. However my OP stated, _"...I don't like using 'public flags' of sorts unless absolutely necessary. I'd rather (if at all possible) inspect the sender or arguments on that event to determine if that property was set server side on reload of data from the database, or if the user actually selected it."_ I know technically what you propose is a `private` flag but it is in the same spirit as using flags to `return.` I was already of that conclusion, but was really looking for something a bit more dynamic as opposed to explicitly setting flags. – atconway Mar 12 '15 at 17:36
0

If I am correct you are trying to set the checked property of check box true on load/initialize of the page.

Here are a couple of approaches you may follow:

  1. Another approach to deal with the situation you mentioned could be to dynamically bind the controls (your checkbox's) event on the page load only if you are not setting the checkbox's checked property to true.
  2. Use a simple page property as a flag to signify if the checked property had been set server side, and use it inside the check box's change event handler. You do not need to use session, as you may not need to persist this value across post back.
  3. If possible put the logic to set the checked property of the check box to true in the check box's change event handler, and based on whether or not you are setting the property you can allow/disallow the handler code.
  4. This would be my recommended solution:

              public bool Check_CheckBox
              {
                set
                {
                    checkbox1.Checked = value;
                    if (value)
                    {
                        checkbox1.CheckedChanged -= new EventHandler(this.Check_Clicked);
                    }
                }
                get
                {
                    return checkbox1.Checked;
                }
            }
    

set the Check_CheckBox property to true based on your logic and it will do the trick.

Hope this helps.

Shashank Chaturvedi
  • 2,756
  • 19
  • 29