By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,222 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
--
I am Tigger.
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
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.
--
I am Tigger.

Nov 19 '05 #2

P: n/a
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.


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
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.
--
I am Tigger.


Nov 19 '05 #3

P: n/a
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.

"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
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.
--
I am Tigger.

Nov 19 '05 #4

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <No****@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
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.
--
I am Tigger.



Nov 19 '05 #5

P: n/a
You didn't mention anything about the AddHandler. Your answer was
misleading and incomplete.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uO**************@TK2MSFTNGP10.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <No****@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Tigger" <Ti****@discussions.microsoft.com> wrote in message
> news:31**********************************@microsof t.com...
>> 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.
>> --
>> I am Tigger.
>
>



Nov 19 '05 #6

P: n/a
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" wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <No****@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
> 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.
> --
> I am Tigger.



Nov 19 '05 #7

P: n/a
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.
"Tigger" <Ti****@discussions.microsoft.com> wrote in message
news:DC**********************************@microsof t.com...
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" wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <No****@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Tigger" <Ti****@discussions.microsoft.com> wrote in message
> news:31**********************************@microsof t.com...
>> 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.
>> --
>> I am Tigger.
>
>


Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.