0

在我的 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>
4

2 回答 2

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。

于 2012-07-02T20:30:49.977 回答
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>"
于 2012-07-02T16:13:45.697 回答