在我的ASP.net应用程序中,在Detailsview上使用ItemInserting, 我正在使用javascript函数向用户显示确认框。确认框基本上显示来自数据库的查询结果。
这样可以正常工作,但即使用户点击取消也会发生回发。
我确定我在这里缺少一些基本的东西,专家可以帮忙吗?感谢您的回复。我在整个网站上搜索过,但大多数解决方案都在按钮上使用onClick事件。我不知道如何在这里完成这项工作。
更新:基于TheGeekYouNeed的建议,我尝试在我的脚本中添加一个条件。现在弹出窗口不再出现了。任何人吗?
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 type="text/javascript">
function confirmBudget(amount) {
return confirm('Based on your last transaction, your amount left is' + amount + '. Do you want to proceed?');
}
</script>
答案 0 :(得分:2)
将此脚本放在aspx页面的head部分。
<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>
从服务器端代码中调用它:
string script = string.Format("<script type='text/javascript'>confirmBudget({0});</script>", TextBox8.Text);
Page.ClientScript.RegisterStartupScript(this.GetType(), "script", script);
对不起C#语法。我不知道vb。
答案 1 :(得分:1)
在您的Javascript中,返回答案。
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>"