CheckBox using an Onclick Event, then Posting Back

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

I have a check box on my page. When a user clicks this box I have a
confirmation box come up.

When the user clicks OK, true is returned, otherwise false.


Now, when true is Returened, I want the form to postback. I can't figure
out how to do this.

Here is the code I use to put the OnClick event on the CHeckbox.

this.chkClosed.Attributes.Add("onClick","return CheckClosed('" +
chkClosed.ID + "');");

It works fine. However, it will not fire a postback.

The Source shows this:

<input id="chkClosed" type="checkbox" name="chkClosed" onclick="return
CheckClosed('chkClosed');__doPostBack('chkClosed','')" language="javascript"
/>

ANy advice?
 
Ryan,

If you function is returning true the postback should certainly fire. May we
see your CheckClosed javascript? The problem must lie there.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
function CheckClosed(checkbox)
{
...var chkBox = document.getElementById(checkbox);
...if(chkBox.checked == true)
...{
.....if(confirm("Changing this Package to Closed will cause all Items within
the Package to be closed.\n Press Ok to Continue."))
........return true;
.....else
........return false;
.....}
...else
...return true;
}

The ConfimationBox pops up correctly, and works fine. It just doesn't
postback. I even hardcoded a "Return true;" at the top of the function and
it didn't fire.

/RT
 
Ryan,

Funny, I sure don't see anything wrong with your code. And placing a return
true at the top of the function is what I would have suggested next. Just to
get an exact description of the behaviour, the page does not post back at
all, or it posts back but the checkbox's event doesn't fire?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
you only want to return if the user click no, otherwise you want to run the
next statement. try:

chkClosed.Attributes.Add("onClick","if (!CheckClosed('" +
chkClosed.ClientID + "') return;");


-- bruce (sqlwork.com)
 
That did it!

Why did this work and not the previous statement I had?

My code worked on a submit button, but not a CheckBox... hmm

Thanks for the help man!

/RT
 
a submit button posts automatically, you cancel in client script by
returning a false on the onclick event. asp.net controls like a checkbox
postback by calling a function __doPostback(), which in turn calls
form.submit(); so with the checkbox you only excute a return to cancel the
postback, but with a submit you return the value true or false to control
the cancel.

-- bruce (sqlwork.com)
 
Back
Top