Solved

ASP.NET call javascript function before postback

Posted on 2009-05-20
3
1,466 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 125 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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