Solved

How to trap the OK cancel click for a client script

Posted on 2010-11-11
10
329 Views
Last Modified: 2012-05-10

I am using the registerclientscript method to display a pop up confirm dialog with OK and cancel butons on some condition wihtin my asp.net page.
How do I trap which button the user clicked within my client code
If gRows.SelectionData.Rows.Count <= 1 Then

                Dim msgDesc As String = "confirm('No codes have been selected.\n\n Click OK to continue.');"                

                ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Confirm", msgDesc, True)               

                Exit Sub

            End If

Open in new window

0
Comment
Question by:countrymeister
  • 5
  • 4
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34110352
"confirm" basically returns true (for OK) or false (for Cancel) so you can either dump the return value somewhere for future use or wrap it in an "if" block;
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34110367
carl_tawn:

Can you please give me a code example, what exactly do I have to do in my above logic
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34110401
Well it depends on what you want to do based on the confirm, but simplisticly:
Dim msgDesc As String = "if (confirm('No codes have been selected.\n\n Click OK to continue.')) DoSomething();"

Open in new window

0
 
LVL 1

Expert Comment

by:mribasu
ID: 34110403
you can do this another way 1st call client side function where you can check if  confirm  true then using __dopostback() come to server side and do somethig or false to  just return.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34110506
carl_tawn:

I tried your example
Dim msgDesc As String = "if (confirm( 'No codes have been selected.\n\n Click OK to continue.')) DoSomething();"

I also added the following in my vb.net code behind
Private Function DoSomething() As Boolean
        Dim str As String = ""
        Return True
    End Function

I get a Jscript error ObjectExpected

I want to some how get to my server side code, so how do I implement the postback to some function or subroutine logic
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34110533
What is it you are getting confirmation for? Is it a button click, or part of another postback? If you want to trigger a postback then you'll probably need to use a hidden control in order to know what condition caused the postback.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34110569
carl_tawn:

I have a submit button. On the click of that submit there iare checks to ensure data entered is valid.
Finally there is a check to see if the user has entered any codes (which is optional)

If no codes have been added, Need to display the confirm dialog box and if user clicks OK, then do rest of the processing.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34110603
I'm guessing you are doing server-side checks rather than client-side?

If that is the case the i would add an invisible button to trigger the postback on confirmation:
<asp:Button ID="ConfirmButton" runat="server" style="display:none;" OnClick="ConfirmButton_Click" />



Then change your script to:



Dim msgDesc As String = "if (confirm('No codes have been selected.\n\n Click OK to continue.')) document.getElementById('" & Me.ConfirmButton.ClientID & "').click();"

Open in new window

0
 
LVL 1

Author Comment

by:countrymeister
ID: 34110680
carl_tawn:

Thanks for your help, that worked.
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 34110681
Thank you so much
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now