2

Basically I am looking to create a JavaScript alert in my code because I am displaying this web application in a mobile browser (off a tablet) and well the normal...

Try
    ' ...
Catch ex As Exception
    Messagebox.Show("Insert Text Here")
End Try

doesn't work in a mobile browser, so I've been told to use a JavaScript alert but I have no idea where to start on this, yes I have used a little JS in the past and still trying to learn it but never came across using an alert. So here is my code below, in which I need to place this alert.

Protected Sub OkBtn_Click(sender As Object, e As System.EventArgs) Handles OkBtn.Click
    Dim ThisDay As Date = Date.Today
    Dim ThisUser As String
    ThisUser = Request.QueryString("")
    If ThisUser = "" Then
        ThisUser = "Chris Heywood"
    End If
    Dim transType As String
    transType = Request.QueryString("")
    If transType = "" Then
        transType = "Fire"
    End If
    connection.Open()
    command = New SqlCommand("Insert Into FireTest([Date],[Type],[Comments],[Completed By],[Trans Type]) Values(@Date,@Type,@Comments,@CompletedBy, @TransType)", connection)
    command.Parameters.AddWithValue("@Date", ThisDay)
    command.Parameters.AddWithValue("@Type", dropdownList1.SelectedValue)
    command.Parameters.AddWithValue("@Comments", TextBox1.Text)
    command.Parameters.AddWithValue("@CompletedBy", ThisUser)
    command.Parameters.AddWithValue("@TransType", transType)
    command.ExecuteNonQuery()
    connection.Close()
    Response.Redirect("~/Production/Navigator.aspx")
End Sub

So basically this alert should be active if there is a error in inserting the information into the database (if there is nothing in the field).

P.S. Would jQuery be viable for this, as it's easy to type, rather than JS?

1 Answer 1

3

You need to understand the difference between server-side code (what you have provided in your question, and is processed on the server) and client-side code (which is what the browser receives from the server and processes on the users machine).

The javascript alert (or window.alert) is a way of putting up a message box with an OK button. If you want a "yes/no" type response, you can use a confirm (or window.confirm) which will return true if OK is selected and false if Cancel is selected (or Escape pressed).

One of the fastest ways of getting what you want it to register script...

Try
   ...  
Catch ex As Exception
   Dim myScript as String = "window.alert('There is a problem');"
   ClientScript.RegisterStartupScript(Me.GetType(), "myScript", myScript, True)
End Try

For more information on this function, see the MSDN entry

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for this but I've just resolved my answer by using a OnClientClick="" in my button the asp.net , answer for trying though.
Fair enough @Kallumasaurus - not sure I deserve the "answer mark" if you've not done what I suggest. And I'm not sure how you've managed to sort a server-side issue out with a client-side call. But I'll take your word for it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.