Using JavaScripts in ASP.NET, please help...

G

Guest

Dear Experts,
I am working on ASP.NET. I have got a problem related to the usage of
Javascript in ASP.NET. Please help. The story is the following:

1) I am developing an ASP.NET application. I need to prompt the users with a
modal box (with "Yes" and "Cancel" button on it);
2) When the user clicks "Yes" button, I need to do some further processing.
If "Cancel" is clicked, of course, stops doing anything;
3) Now the problems are:
- which JavaScript would be appropriate? e.g. confirm, showModalDialog, etc.
- * How can I catch the click event in ASP.NET codes when the user clicks
the "YES" or "Cancel" button? This is the most tricky part that I really
don't know on how to do so! Please kindly help.

Thanks a lot.
 
K

Karl Seguin

Tigger:
The easiest way is to use a normal asp:net button and attach a javascript
event to it:

<asp:button id="delete" runat="server" />

in your codebehind, you do:

Sub Page_Load
delete.Attributes.Add("onClick", "return confirm('Are you sure?');")
End Sub

and the processing can happen in the delete's button server-side click
event...which will only fire if they clicked "ok" in the confirm window.

http://aspnet.4guysfromrolla.com/articles/021104-1.aspx as the steps in
greater detail.

Karl
 
S

Scott M.

Your code will not result in the button's server side click event being
handled at all. What you've showed is how to attach a client-side event
handler at the server level.
 
S

Scott M.

You do this in client side JavaScript as usual. ASP.NET is a server-side
programming paradigm. What you want is client-side functionality. You can
attach a client-side event handler from the server side, but client events
do not trigger server events.

What you could do, is write your own client-side submit function that
submits the form with a hidden form field that indicates something known at
the client level.
 
K

Karl Seguin

I musn't have explained myself or am totally missing something

What I got from the question was: "How do I display a yes/no type box and
when "yes" is clicked proceed to do some processing (presumably on the
server side)"

My solution was something along the lines of:

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button ID="delete" Runat="server" />
</form>
</body>
</HTML>
<script runat="server">
public sub page_load
delete.Attributes.Add("onClick", "return confirm('are you sure?');")
AddHandler delete.Click, AddressOf Delete_Clicked
end sub

private sub Delete_Clicked(sender as object, e as EventArgs)
Response.Write("HERE!")
end sub
</script>


which indeeds work. Perhaps what I was missing was the AddHandler, but I
don't know how Tigger prefers to set up his events and figured it was kinda
self-evident (since I did use an asp:button after all) so I left it out
(note however that I did point to a more complete tutorial).

I'm not really sure how your idea of creating your own hidden variables and
your own submission code is any better than this.

Anyways, I might be out to lunch...

Karl
 
S

Scott M.

You didn't mention anything about the AddHandler. Your answer was
misleading and incomplete.


Karl Seguin said:
I musn't have explained myself or am totally missing something

What I got from the question was: "How do I display a yes/no type box and
when "yes" is clicked proceed to do some processing (presumably on the
server side)"

My solution was something along the lines of:

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button ID="delete" Runat="server" />
</form>
</body>
</HTML>
<script runat="server">
public sub page_load
delete.Attributes.Add("onClick", "return confirm('are you sure?');")
AddHandler delete.Click, AddressOf Delete_Clicked
end sub

private sub Delete_Clicked(sender as object, e as EventArgs)
Response.Write("HERE!")
end sub
</script>


which indeeds work. Perhaps what I was missing was the AddHandler, but I
don't know how Tigger prefers to set up his events and figured it was
kinda
self-evident (since I did use an asp:button after all) so I left it out
(note however that I did point to a more complete tutorial).

I'm not really sure how your idea of creating your own hidden variables
and
your own submission code is any better than this.

Anyways, I might be out to lunch...

Karl
 
G

Guest

Hello Experts,
Thanks a lot for your reply. Your example really works but it needs me to
click a button in order to kick off the action (prompt out a Confirm message
box). How about if I would like to do the following:
1) call a function (e.g. myFunction). This function returns a string;
2) immediately right after getting the result from myFunction, I would like
to prompt the Confirm message box to the users with the result shown on the
box;
3) if the user clicks YES, then go ahead to do some other processing on the
server;

*As you can see, no button is clicked in the above flow. Is it possible to
do so?
Please let me know.
Many thanks.
Tigger


Karl Seguin said:
I musn't have explained myself or am totally missing something

What I got from the question was: "How do I display a yes/no type box and
when "yes" is clicked proceed to do some processing (presumably on the
server side)"

My solution was something along the lines of:

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button ID="delete" Runat="server" />
</form>
</body>
</HTML>
<script runat="server">
public sub page_load
delete.Attributes.Add("onClick", "return confirm('are you sure?');")
AddHandler delete.Click, AddressOf Delete_Clicked
end sub

private sub Delete_Clicked(sender as object, e as EventArgs)
Response.Write("HERE!")
end sub
</script>


which indeeds work. Perhaps what I was missing was the AddHandler, but I
don't know how Tigger prefers to set up his events and figured it was kinda
self-evident (since I did use an asp:button after all) so I left it out
(note however that I did point to a more complete tutorial).

I'm not really sure how your idea of creating your own hidden variables and
your own submission code is any better than this.

Anyways, I might be out to lunch...

Karl
 
M

MWells

Tigger, there are a number of ways to approach this.

As Karl and Scott pointed out, there is a very clean way to add a
confirmation dialog to an ASP.NET button, that cancels the postback if you
cancel the dialog. If "myFunction" is a javascript function, you can easily
embed this in your page using the same mechanism they've described. As long
as your function returns true/false, you can use your function to approve
(true) or cancel (false) the button-click operation.

If you need more complex processing, database queries, or for other reasons
want myFunction to be in your .NET codebase, then you will probably be
better off building a WebForm that provides the messagebox-like capability
you desire.

Go with a pattern you're comfortable with. You might do something
wizard-style; a sequence of pages with server-side logic to dictate the
navigation rules. Or you might go with a pop-up HTML page that you style
like a message box. The latter approach will require some javascript to
display the window, and probably some more to refresh your screen or perform
some navigation once the "message box" response is captured.

In short, developing a web app still doesn't come with all the conviences
that Windows application developers are accustomed to. HTTP and HTML were
not originally designed to support apps, but with enough creativity and
research you can still mimic most of the familiar GUI features.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top