views:

55

answers:

2

I have an ASP.NET checkbox, and I want to run some javascript before it posts back.

Something like:

<asp:CheckBox ID="chkSelected" runat="server" AutoPostBack="True" OnCheckedChanged="chkSelected_CheckedChanged" />

but I want to run a JS confirm dialog when the user click it, and if they were UNCHECKING it, I'd like to ask them if they're sure, and if so, postback and run my serverside code. If they say No, don't postback at all.

Anyone know of an 'easy' way to do this?

A: 

I haven't tried this with a checkbox but try to see if there is an OnClientClick attribute for your asp:CheckBox. If there is, put the name of the javascript function in it. Then in your javascript you can return true if the postback should happen or return false if you want to cancel the postback.

ZippyV
Unfortunately no. That's only for Buttons, LinkButtons, ImageButtons etc. Which is too bad because that works exactly like what I want...
LoveMeSomeCode
The other option I would think about is the JQuery code that SLaks posted.
ZippyV
+2  A: 

You can add an onclick handler:

<asp:CheckBox ... onclick="if (!this.checked && !confirm('Are you sure?')) return false;" />

(I believe this will work, but I didn't try it)

You can also add an event in Javascript. (which is more likely to work)
Using jQuery:

$('#<%=chkSelected.ClientID%>').click(function() {
    return this.checked || confirm('Are you sure?');
});
SLaks
But this doesn't cause it to postback does it? I need it to just do nothing if they click No, and if they click Yes it should cause a postback and run my serverside 'checked changed' event. Which I think requires it having the '__doPostBack()' method in the onclick method.
LoveMeSomeCode
If the handler returns true, it should postback from the inline handler. Try it.
SLaks
yep that did it! I'm a little confused as to why though...Your JS: if (!this.checked will cause a postback if they click Yes, and no postback if they click No.My JS: return confirm('Are you sure?'); will never cause a postback no matter what you choose.Why is this?Thanks either way!
LoveMeSomeCode
By writing `return ...;`, you're exiting the `onclick` handler before ASP.Net's `__doPostBack()` call (which comes right after your `return` statement). You should only `return` at all if you want to suppress the postback. Look at the generated source in the browser.
SLaks
That's what I would have thought, but while I was waiting, I went another route and used an ImageButton control. I set OnClick="ibSelected_Click". Then in the Page_Load i put this:ibSelected.OnClientClick = "return confirm('Are you sure?');";and what shows up in the generated source is this:<input type="image" onclick="return confirm('Are you sure?');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(...etc. which works, Click Yes->postback, Click No->no postback.I'm surprised that this works.
LoveMeSomeCode
That's because the default behavior of an `<input type="image">` is to submit the `<form>`. However, an `<input type="check">` will not submit the `<form>` automatically. (Unless the Javascript handler submits it manually)
SLaks
Good to know. What I don't know about JS could fill SO. On that note I just ordered Javascript the Good Parts from amazon; it came pretty highly recommended on here. Thanks again!
LoveMeSomeCode