Solved

ASP.NET call javascript function before postback

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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