asp.net onClick event for Button

565 Views Asked by At

I have a button on a web page that when clicked connects to my SQL server and runs a stored procedure. The buttons works fine (It simply pushes data into a database table).

I’m new to .NET programming and I’m having trouble finding the right code ideas to add on to the current code for the button I already have.

Ideally, I would just like to have a message box to appear when the button is clicked to notify the user that the ‘data has been saved’ with just the "OK" option and for the user to then be redirected to the home page.

I also need to make sure that this button is only pressed once or deactivated once it has been pressed that day as I don’t want the user to press the button multiple times.

Here is my current code for the button:

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

Any ideas and suggestions would be much appreciated.

Regards, Betty.

1

There are 1 best solutions below

5
On BEST ANSWER
function success()
{

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

On code behind, right after this line:

dbcommand.ExecuteNonQuery()

Do this:

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())