0

In my ASP.net application, using ItemInserting on a Detailsview, I am using a javascript function to display an confirm box to the user. The confirm box basically displays the result of a query from the database.

THis works fine, except that postback ooccurs even if the users click cancel.

Im sure I am missing something elementary here , can the experts help? Your response is appreciated. I did search all over the site, but most of the solutions use an onClick event on a button. I am not sure how to make that work here.

Update: based on TheGeekYouNeed suggestion, I tried putting in a condition in my script. Now the popup does not come up anymore. Anyone?

 Protected Sub CustomerDetail_ItemInserting(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)
    Dim args As String = e.Values("WriteID").ToString()

    For Each drv As Data.DataRowView In SqlDataSource3.[Select](DataSourceSelectArguments.Empty)
        If drv("WriteID") = args Then


            Dim ConnString As String = "Data Source=75409CHQ4034\SQLEXPRESS;Initial Catalog=ExpenseTracker;Integrated Security=True"
            Dim SQLConn As New SqlConnection()
            Dim SQLCmd As New SqlCommand()
            Dim SQLdr As SqlDataReader

            SQLConn.ConnectionString = ConnString 'Set the Connection String
            SQLConn.Open() 'Open the connection
            SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
            SQLCmd.CommandText = "Select  Top 1 Budget - Difference as 'Remaining' FROM ExpenseImageAudit where WriteID =" + "'" + args + "' Group by ID, Budget-Difference order by ID desc "

            'Sets the SQL String
            SQLdr = SQLCmd.ExecuteReader 'Gets Data

            While SQLdr.Read() 'While Data is Present

                TextBox8.Text = SQLdr("Remaining")

                Dim script As String = String.Format("<script type='text/javascript'>confirmBudget({0});</script>", TextBox8.Text)
                Page.ClientScript.RegisterStartupScript(Me.[GetType](), "script", script)
            End While
        SQLdr.Close() 'Close the SQLDataReader
        SQLConn.Close() 'Close the connection

        End If

    Next
End Sub

Script:

    <script type="text/javascript">
    function confirmBudget(amount) {
        return confirm('Based on your last transaction, your amount left is' + amount + '. Do you want to proceed?');
    }        
</script>
Sridhar R
  • 13
  • 7
  • return false when user clicks on cancel to prevent postback – Asdfg Jul 02 '12 at 16:10
  • Not sure how to do that. Can you give an example? – Sridhar R Jul 02 '12 at 16:13
  • **Dim script As String = ""** Page.ClientScript.RegisterStartupScript(Me.[GetType](), "script", script)** – Sridhar R Jul 02 '12 at 18:46
  • you cant just return false everytime. TheGeekYouNeed's answer should work. – Asdfg Jul 02 '12 at 18:55
  • Well, its not working. :) This doesnt work either: 'Dim script As String = "" 'Page.ClientScript.RegisterStartupScript(Me.[GetType](), "script", script) – Sridhar R Jul 02 '12 at 18:59
  • That's not the answer I gave. You revised the javascript. It's wrong. Use exactly what I put. – TheGeekYouNeed Jul 02 '12 at 19:16
  • Dim script As String = "" – Sridhar R Jul 02 '12 at 19:48

2 Answers2

2

Put this script in the head section of your aspx page.

<script type="text/javascript">
   function confirmBudget(amount)
   {
      if(confirm('Based on your last transaction, your amount left is' + amount + '. Do you want to proceed?'))
      {
         document.getElementById("btnDoSomethingOnOK").click();
      }

   }
</script>

Call it like this from your server side code:

string script = string.Format("<script type='text/javascript'>confirmBudget({0});</script>", TextBox8.Text);

Page.ClientScript.RegisterStartupScript(this.GetType(), "script", script);

Sorry for the C# syntax. I dont know vb.

Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • Asdfg, i tried your solution. I dont get any errors and the popup comes up but the value is again posted into the database on clicking cancel. Updated code above. – Sridhar R Jul 02 '12 at 21:01
  • What does btnDoSomethingOnOK refer to? Since this is a DeailsView form, would it be the insert button? – Sridhar R Jul 02 '12 at 21:35
  • its the dummy button that you may want to execute if user clicks "Ok" – Asdfg Jul 03 '12 at 02:13
1

In your Javascript, return answer.

Dim script As String = "<script language='javascript'>var a=' " + TextBox8.Text & "'; var answer = confirm('Based on your last transaction, your amount left is '+ a + '. Do you want to proceed?'); return answer; </script>"
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
  • Return answer is not working. Now the popup does not come up at all and the debugger error is "return statement outside function" – Sridhar R Jul 02 '12 at 16:24
  • While SQLdr.Read() TextBox8.Text = SQLdr("Remaining") ' Dim script As String = "" Page.ClientScript.RegisterStartupScript(Me.[GetType](), "script", script) End While – Sridhar R Jul 02 '12 at 16:53
  • The statement is part of the script...do i need to declare elsewhere? – Sridhar R Jul 02 '12 at 17:33
  • TheGeekYouNeed , thanks for your reply. I edited it because i was getting the "return statement outside function" error. I used your edited version again and am still getting that error. – Sridhar R Jul 02 '12 at 19:47