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

Discussion in 'ASP .Net' started by =?Utf-8?B?VGlnZ2Vy?=, Jan 11, 2005.

  1. 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.
    =?Utf-8?B?VGlnZ2Vy?=, Jan 11, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?VGlnZ2Vy?=

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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.
    Karl Seguin, Jan 11, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?VGlnZ2Vy?=

    Scott M. Guest

    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:%...
    > 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" <> wrote in message
    > news:...
    >> 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.

    >
    >
    Scott M., Jan 11, 2005
    #3
  4. =?Utf-8?B?VGlnZ2Vy?=

    Scott M. Guest

    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" <> wrote in message
    news:...
    > 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.
    Scott M., Jan 11, 2005
    #4
  5. =?Utf-8?B?VGlnZ2Vy?=

    Karl Seguin Guest

    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." <> wrote in message
    news:%23lyoP$...
    > 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:%...
    > > 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" <> wrote in message
    > > news:...
    > >> 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.

    > >
    > >

    >
    >
    Karl Seguin, Jan 11, 2005
    #5
  6. =?Utf-8?B?VGlnZ2Vy?=

    Scott M. Guest

    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:...
    >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." <> wrote in message
    > news:%23lyoP$...
    >> 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:%...
    >> > 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" <> wrote in message
    >> > news:...
    >> >> 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.
    >> >
    >> >

    >>
    >>

    >
    >
    Scott M., Jan 12, 2005
    #6
  7. 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." <> wrote in message
    > news:%23lyoP$...
    > > 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:%...
    > > > 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" <> wrote in message
    > > > news:...
    > > >> 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.
    > > >
    > > >

    > >
    > >

    >
    >
    >
    =?Utf-8?B?VGlnZ2Vy?=, Jan 12, 2005
    #7
  8. =?Utf-8?B?VGlnZ2Vy?=

    MWells Guest

    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" <> wrote in message
    news:D...
    > 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." <> wrote in message
    > > news:%23lyoP$...
    > > > 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:%...
    > > > > 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" <> wrote in message
    > > > > news:...
    > > > >> 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.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >
    > >
    MWells, Jan 12, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    552
    Big Brian
    Oct 14, 2003
  2. Julia Briggs

    Calling multiple javascripts -- help!

    Julia Briggs, Jul 28, 2003, in forum: Javascript
    Replies:
    5
    Views:
    98
    Richard Cornford
    Aug 2, 2003
  3. sdvoranchik
    Replies:
    1
    Views:
    135
    Thomas 'PointedEars' Lahn
    Jan 31, 2004
  4. JCO

    Downloads Using JavaScripts

    JCO, Dec 5, 2004, in forum: Javascript
    Replies:
    6
    Views:
    117
  5. stroumf
    Replies:
    2
    Views:
    151
    Randy Webb
    Nov 8, 2006
Loading...

Share This Page