Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Using JavaScripts in ASP.NET, please help...

Reply
Thread Tools

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

 
 
=?Utf-8?B?VGlnZ2Vy?=
Guest
Posts: n/a
 
      01-11-2005
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.
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      01-11-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      01-11-2005
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:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-11-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      01-11-2005
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." <(E-Mail Removed)> wrote in message
news:%23lyoP$(E-Mail Removed)...
> 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:%(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> 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.

> >
> >

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-12-2005
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:(E-Mail Removed)...
>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." <(E-Mail Removed)> wrote in message
> news:%23lyoP$(E-Mail Removed)...
>> 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:%(E-Mail Removed)...
>> > 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" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> >> 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.
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?VGlnZ2Vy?=
Guest
Posts: n/a
 
      01-12-2005
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." <(E-Mail Removed)> wrote in message
> news:%23lyoP$(E-Mail Removed)...
> > 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:%(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > >> 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.
> > >
> > >

> >
> >

>
>
>

 
Reply With Quote
 
MWells
Guest
Posts: n/a
 
      01-12-2005
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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." <(E-Mail Removed)> wrote in message
> > news:%23lyoP$(E-Mail Removed)...
> > > 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:%(E-Mail Removed)...
> > > > 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" <(E-Mail Removed)> wrote in message
> > > > news:(E-Mail Removed)...
> > > >> 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.
> > > >
> > > >
> > >
> > >

> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically loading and using javascripts in firefox stroumf Javascript 2 11-08-2006 04:47 PM
Javascripts retrieved via WebResource.axd nick@codefresh.com ASP .Net 0 01-20-2006 09:16 AM
Downloads Using JavaScripts JCO Javascript 6 12-06-2004 03:36 PM
Frames using javascripts to open external sites cause problems in IE (not Netscape) sdvoranchik Javascript 1 01-31-2004 10:09 AM
Re: Including javascripts in HTML - it appears as the script never loads Bob Walton Perl 0 07-19-2003 02:30 AM



Advertisments