Solved

ASP.NET call javascript function before postback

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML table 7 54
Connection String to remote Server not working 3 57
INNER JOIN ? 8 33
Achieve json result 2 35
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

13 Experts available now in Live!

Get 1:1 Help Now