1

I have a custom asp.net server control (actually a specialized version of a checkbox). I want to do some processing on a postback but only if the postback was a result of this control being clicked (with autopostback being set to true). What is the best way to determine if the postback is a result of this control or something else?

Thanks for your help.

Paul Hyman
  • 81
  • 1
  • 7
  • Couldn't you just create a click handler for your custom object? It will be called automatically after the page posts back. – Cᴏʀʏ Jul 24 '09 at 13:25
  • I don't believe that custom server controls receive a click event. At least I haven't been able to find anything like that. I think click events are generated by button controls when they determine that they were the control that caused the postback. Of course I may be missing something here.. – Paul Hyman Jul 24 '09 at 15:29

2 Answers2

1

You might be able to get this to work:

Build logic that tells you which control posted back:

http://www.eggheadcafe.com/articles/20050609.asp

Cory Larson comment is also a good one... :)

BigBlondeViking
  • 3,853
  • 1
  • 32
  • 28
1

It sounds like you could use event bubbling. Basically you would just expose the onchange event in your checkbox (if your server control is a composite control) from you custom server control. Then write your special handling code in an eventhandler in the page hosting the control.

HectorMac
  • 6,073
  • 4
  • 24
  • 24
  • My control is just a class that derives from Checkbox. I can't find any onchange event.. – Paul Hyman Jul 24 '09 at 15:44
  • If it is an "asp:CheckBox", then the event is called OnCheckedChanged(). After you have implmented the code to bubble the event, you would call the RaiseBubbleEvent() OnCheckChanged() event handler. – HectorMac Jul 24 '09 at 16:48
  • Thanks for taking the time to answer. Actually the reason I'm doing this is because the CheckChanged event doesn't always get raised when I need it to. If the control believes the check state hasn't changed since the last postback then no event. But if some javascript code changed the state of the checkbox without causing a postback and then the user clicked in the box again, the server code won't know the state actually changed (via javascript) and then changed back when the user clicked in the box. It will just see that the state is the same as the last postback and not raise the event. – Paul Hyman Jul 24 '09 at 17:18