Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET call javascript function before postback

Posted on 2009-05-20
3
Medium Priority
?
1,478 Views
Last Modified: 2012-05-07
I know this has been asked may times before, but it's not working for me.
Using ASP.NET 2.0 / VB:
I simply want to pop a javascript confirm box from an event before a post if a condition is true.

If iCount > 0 Then

  Dim strJSconfirm As String
     strJSconfirm = "<script type='text/javascript'>" & vbCrLf
     strJSconfirm += "function delRC(){" & vbCrLf
     strJSconfirm += "confirm('are you sure you want to delete?');}" & vbCrLf
     strJSconfirm += "</script>"

     ClientScript.RegisterStartupScript(Page.GetType(), "jsDelRC", strJSconfirm)

End If

But it just refreshes the page, and writes the code block to it.
How do I get it to fire?
0
Comment
Question by:el_scotty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24433562
when/where do you want it to fire? this would kinda do it right here if you just want it to fire when the condition is true:
If iCount > 0 Then
 
  Dim strJSconfirm As String
     strJSconfirm = "<script type='text/javascript'>" & vbCrLf
     strJSconfirm += "function delRC(){" & vbCrLf
     strJSconfirm += "confirm('are you sure you want to delete?');}" & vbCrLf
     strJSconfirm += "delRC();" & vbcrlf
     strJSconfirm += "</script>"
 
     ClientScript.RegisterStartupScript(Page.GetType(), "jsDelRC", strJSconfirm)
 
End If

Open in new window

0
 
LVL 1

Author Comment

by:el_scotty
ID: 24436749
Thanks,
Don't know how I missed the obvious there. That did alllow the js to execute, however I was hoping it would keep any code after the RegisterStartupScript from executing until confirmed.

The code above is in an event fired when user clicks an image button biound to a repeater control, which: Checks the database, if condition is met (iCount > 0) then confim dialog.
I want to: Cancel ->Halt or OK->Continue

So is it possible to do this, in this approach. I have yet to integrate AJAX so I'm looking to do it w the familiar. I guess I could have the js above fire another event (hidden button), but just thought I could keep it more simple.
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 375 total points
ID: 24440158
ahh... yes i was wondering... in that case, this will do it:
If iCount > 0 Then
 
  Dim strJSconfirm As String
     strJSconfirm = "<script type='text/javascript'>" & vbCrLf
     strJSconfirm += "function delRC(){" & vbCrLf
     strJSconfirm += "if(confirm('are you sure you want to delete?')) & VBCrlf
     strJSconfirm += "{CALL AJAX FUNCTION!}" & vbcrlf
     strJSconfirm += "return}" & vbCrLf
     strJSconfirm += "delRC();" & vbcrlf
     strJSconfirm += "</script>"
 
     ClientScript.RegisterStartupScript(Page.GetType(), "jsDelRC", strJSconfirm)
 
End If

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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