views:

569

answers:

3

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.

+1  A: 

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
+1  A: 

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
My control is just a class that derives from Checkbox. I can't find any onchange event..
Paul Hyman
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
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
+3  A: 

This May Help

cptScarlet
That has some good information. However, it states:"Having the name of the control, we can easily get the reference to the instance of that control via FindControl method of the Page object."Since my code is in a server control which is not derived from the Page, do I have access to this?
Paul Hyman
Just discovered I can reference the Page object. So this looks promising.
Paul Hyman
Not only tnat but in poking around in the debugger I found an (apparently undocumented) property of the Page called AutoPostBackControl, so all I had to do was check If Page.AutoPostBackControl = Me (in VB).
Paul Hyman
Forget AutoPostBackControl - it sometimes has the wrong control. This works better:If Page.Request.Form("__EVENTTARGET") = Me.UniqueID Then ...
Paul Hyman