【问题标题】:asp.net onClick event for Button按钮的asp.net onClick事件
【发布时间】:2011-10-28 20:20:22
【问题描述】:

我在网页上有一个按钮,单击该按钮可连接到我的 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

任何想法和建议将不胜感激。

问候, 贝蒂。

【问题讨论】:

    标签: asp.net asp.net-controls


    【解决方案1】:
    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())
    

    【讨论】:

    • window.location 将不起作用,除非您指定协议。使用window.location.href = "default.aspx"
    • 大家好,非常感谢以上内容。这是一种享受。我确实在代码的 window.location.href 部分遇到了问题,但是用 'Response.Redirect("~/default.aspx?") 替换了它,并且效果一样。
    • @Betty 很高兴你能正常工作。请采纳答案;这将鼓励 SO 中更多的人在未来为您提供帮助。
    • @Icarus,要接受,我只需点击“是”即可“这篇文章对您有用吗?”
    • @Betty,您应该会在数字 0 下方看到一个带有向上/向下箭头的复选标记。只需单击复选标记即可接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多