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
Solved

ASP.NET call javascript function before postback

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 37
Hide Table in merge 3 29
FInd Image Control Gridview 3 19
CREATE DATABASE 3 28
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

766 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