CheckBox using an Onclick Event, then Posting Back

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?
 
S

S. Justin Gengo

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
 
R

Ryan Ternier

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
 
S

S. Justin Gengo

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
 
B

Bruce Barker

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)
 
R

Ryan Ternier

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
 
B

Bruce Barker

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top