Solved

How to trap the OK cancel click for a client script

Posted on 2010-11-11
10
332 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

808 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