问题
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:
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
来源:https://stackoverflow.com/questions/22245627/using-a-javascript-alert-in-vb-net