用于Button的asp.net onClick事件

时间:2011-10-28 09:59:02

标签: asp.net asp.net-controls

我在网页上有一个按钮,当点击它时连接到我的SQL服务器并运行存储过程。按钮工作正常(它只是将数据推送到数据库表)。

我是.NET编程的新手,我无法找到正确的代码提示添加到我已有的按钮的当前代码中。

理想情况下,我只想在单击按钮时显示一个消息框,通过“确定”选项通知用户“数据已保存”,然后将用户重定向到主页。

我还需要确保此按钮只按一次或在当天按下时停用,因为我不希望用户多次按下按钮。

以下是该按钮的当前代码:

Protected Sub btnAddRawData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRawData.Click

    'database conn, this is linked to the web config file .AppSettings
    Using dbconnection As New SqlConnection(ConfigurationManager.AppSettings("dbconnection"))
        dbconnection.Open()

        'command to state the stored procedure and the name of the stored procedure
        Using dbcommand As SqlCommand = dbconnection.CreateCommand
            With dbcommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "GasNominationsRawData_Insert"

                'simply execute the query
                dbcommand.ExecuteNonQuery()

                'I need to add some code for the button here and...

                'redirect to the main home page
                Response.Redirect("~/default.aspx?")

            End With
        End Using
    End Using
End Sub

非常感谢任何想法和建议。

此致 贝蒂。

1 个答案:

答案 0 :(得分:0)

function success()
{

 alert('Your data has been saved successfully)';
 window.location.href='Default.aspx';
}

在代码背后,就在这一行之后:

dbcommand.ExecuteNonQuery()

这样做:

Dim cs As ClientScriptManager = Page.ClientScript
Dim csname1 As [String] = "PopupScript"
Dim cstype As Type = Me.[GetType]()
Dim cstext1 As New StringBuilder()
cstext1.Append("success();")
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString())