最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Using a Javascript alert in VB.Net - Stack Overflow

programmeradmin2浏览0评论

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()
    mand = New SqlCommand("Insert Into FireTest([Date],[Type],[Comments],[Completed By],[Trans Type]) Values(@Date,@Type,@Comments,@CompletedBy, @TransType)", connection)
    mand.Parameters.AddWithValue("@Date", ThisDay)
    mand.Parameters.AddWithValue("@Type", dropdownList1.SelectedValue)
    mand.Parameters.AddWithValue("@Comments", TextBox1.Text)
    mand.Parameters.AddWithValue("@CompletedBy", ThisUser)
    mand.Parameters.AddWithValue("@TransType", transType)
    mand.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?

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()
    mand = New SqlCommand("Insert Into FireTest([Date],[Type],[Comments],[Completed By],[Trans Type]) Values(@Date,@Type,@Comments,@CompletedBy, @TransType)", connection)
    mand.Parameters.AddWithValue("@Date", ThisDay)
    mand.Parameters.AddWithValue("@Type", dropdownList1.SelectedValue)
    mand.Parameters.AddWithValue("@Comments", TextBox1.Text)
    mand.Parameters.AddWithValue("@CompletedBy", ThisUser)
    mand.Parameters.AddWithValue("@TransType", transType)
    mand.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?

Share Improve this question edited Jun 23, 2020 at 11:14 Andrew Morton 25.1k9 gold badges65 silver badges93 bronze badges asked Mar 7, 2014 at 9:00 KallumasaurusKallumasaurus 2711 gold badge4 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论