Javascript Interaction with asp.net controls

G

Guest

Hi People,

I have got a simple question to ask. I am trying to build dynamic controls
in asp.net. I have managed to build them successfully. I have also managed to
add the event handlers to all controls I want to monitor. I have the
following code line: -

((CheckBox)ctl).Attributes["onclick"] =
"if(!this.checked){input_box=confirm('All changes to Test will be lost');}";

This is just enabling an action on the check box to be monitored. A
confirmation box props up. Now what I want to do is that if the user selects
ok only then I want to perform the action, but if he presses cancel, then I
do not want to do anything at all. Using this "onclick" thing how can I check
to see what the user selected. Presently it performs the action even if the
user selects cancel.

I hope you guys can help me out.

Much Thanks,

Irfan
 
E

Eliyahu Goldin

Irfan,

Are you talking about client or server action? For client action just right
a javascript and call it on onclick event. For server actions you need to
pass an action code from client to server. The most common way of doing this
is using a hidden <input> element.

Eliyahu
 
G

Guest

Hi Goldin,

First of all thank you for your response. You said in your answer "just right
a javascript and call it on onclick event". Do we write the code in c# or
actual javascript, if javascript then won't the compiler complain ? If in
javascript then how do we reference the c# variables in the javascript code ?

Many thanks,

Irfan
 
G

Guest

Hi David,

What I'm trying to do eventually is that if the user deselects any of the
check boxes, then there should a pop-up message to confirm the users action.
Only if the user selects yes, then the event happens, not otherwise. You see
to display the pop-up confirmation box i need to use javascript i.e.

((CheckBox)ctl).Attributes["onclick"] =
"if(!this.checked){input_box=confirm('All changes to Test will be lost');}";

Now the question is, how to get what the user selected back in to my c# code.

Hope you understand what i'm trying to say...

Will wait for your response....

Thanks,

Irfan

David Jessee said:
I might be out in left field here...I'm not totally sure what you're wanting
to do...

Why can you not just check yourCheckBox.Checked at the server side?


Irfan Akram said:
Hi People,

I have got a simple question to ask. I am trying to build dynamic controls
in asp.net. I have managed to build them successfully. I have also managed to
add the event handlers to all controls I want to monitor. I have the
following code line: -

((CheckBox)ctl).Attributes["onclick"] =
"if(!this.checked){input_box=confirm('All changes to Test will be lost');}";

This is just enabling an action on the check box to be monitored. A
confirmation box props up. Now what I want to do is that if the user selects
ok only then I want to perform the action, but if he presses cancel, then I
do not want to do anything at all. Using this "onclick" thing how can I check
to see what the user selected. Presently it performs the action even if the
user selects cancel.

I hope you guys can help me out.

Much Thanks,

Irfan
 
D

David Jessee

I might be out in left field here...I'm not totally sure what you're wanting
to do...

Why can you not just check yourCheckBox.Checked at the server side?
 
E

Eliyahu Goldin

Irfan,

Client side doesn't know any c#, only javascript. You can write it in aspx
page. Compiler won't complain since compiler compiles server code. Client
javascript can't reference server c# variables. It can pass information to
server side, where you read it in c# code. When working in asp.net, you have
to understand clearly what's going on the server and what on the client.

Eliyahu
 
D

David Jessee

The the user checks the checkbox, then if they click yes, you want the
ASP.NET postback to happen (Complete with the checkbox's event handler), and
if they click no, you don't want the postback to happen?


Irfan Akram said:
Hi David,

What I'm trying to do eventually is that if the user deselects any of the
check boxes, then there should a pop-up message to confirm the users action.
Only if the user selects yes, then the event happens, not otherwise. You see
to display the pop-up confirmation box i need to use javascript i.e.

((CheckBox)ctl).Attributes["onclick"] =
"if(!this.checked){input_box=confirm('All changes to Test will be lost');}";

Now the question is, how to get what the user selected back in to my c# code.

Hope you understand what i'm trying to say...

Will wait for your response....

Thanks,

Irfan

David Jessee said:
I might be out in left field here...I'm not totally sure what you're wanting
to do...

Why can you not just check yourCheckBox.Checked at the server side?


Irfan Akram said:
Hi People,

I have got a simple question to ask. I am trying to build dynamic controls
in asp.net. I have managed to build them successfully. I have also
managed
to
add the event handlers to all controls I want to monitor. I have the
following code line: -

((CheckBox)ctl).Attributes["onclick"] =
"if(!this.checked){input_box=confirm('All changes to Test will be lost');}";

This is just enabling an action on the check box to be monitored. A
confirmation box props up. Now what I want to do is that if the user selects
ok only then I want to perform the action, but if he presses cancel,
then
I
do not want to do anything at all. Using this "onclick" thing how can
I
check
to see what the user selected. Presently it performs the action even
if
the
user selects cancel.

I hope you guys can help me out.

Much Thanks,

Irfan
 

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,007
Latest member
obedient dusk

Latest Threads

Top