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>