在Asp.net 3.5后面的代码中的Java脚本警报消息

时间:2009-09-26 08:34:26

标签: asp.net javascript vb.net

如何在代码后面使用Java脚本警报消息?保存数据后将显示此消息。

这是我的代码,

enter code here

 Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add.Click
    'add new class
    Dim DbClassMst = New ClassMst()
    Dim dsClass As DataSet
    Dim status As Integer = 0
    Dim ResourceObject As Object

    ' 画面リソース定義
    If SessionCtl.GetSession("RES_LANG").ToString = ResourceBase.LOCALE_JA Then
        ResourceObject = New ResourceJa(SessionCtl.GetSession("RES_LANG"))
    Else
        ResourceObject = New ResourceEn(SessionCtl.GetSession("RES_LANG"))
    End If

    If NewClass.Text = "" Then

        lblErrMsg.Text = ResourceObject.getMsg(ResourceBase.NEW_CLASS_REG)
        Exit Sub
    Else
        'check to allow unique class
        dsClass = DbClassMst.DisplayClass1(NewClass.Text)
        If Trim(dsClass.Tables(0).Rows.Count > 0) Then
            status = 1
        End If
        If dsClass.Tables(0).Rows.Count < 10 Then
            If status = 0 Then
                'insert class
                DbClassMst.InsertClassNew(NewClass.Text, dsClass.Tables(0).Rows.Count + 1)
                PopulateClassName()
                NewClass.Text = ""


                Dim AlertMsg As String = ""
                AlertMsg = "<script language='javascript'>alert('Data has been saved');</script>"

                 *********Here I need alert msg.



            Else
                lblErrMsg.Text = ResourceObject.getMsg(ResourceBase.NEW_CLASS_EXIST)
                Exit Sub
            End If

        Else
            lblErrMsg.Text = ResourceObject.getMsg(ResourceBase.NEW_CLASS_MAX)
        End If
    End If

End Sub

但不起作用。给我一个主意。

3 个答案:

答案 0 :(得分:6)

更好的方法是将其作为公共类中的实用工具方法:

Friend Module MyUtilities
    Public Sub Alert(ByVal page As Page, ByVal message As String)
        Dim alertMessage As String = "alert('" & message & "');"
        page.ClientScript.RegisterStartupScript(page.[GetType](), "showAlert", alertMessage, True)
    End Sub
End Module

这可以在以下页面中使用:

 MyUtilities.Alert(Me, "Sample alert!!!!!")

您的代码看起来更简单。希望这有帮助!

答案 1 :(得分:2)

您应该将此脚本注册到您的页面,以便将此脚本发送给客户端。

Page.ClientScript.RegisterStartupScript(typeof(YourPage), 
     "myScripts", 
     "<script language='javascript'>alert('Data has been saved');</script>");

答案 2 :(得分:0)

Dim someScript As String =“” someScript =“alert('你的请假申请已经注册');” Page.ClientScript.RegisterStartupScript(Me.GetType(),“onload”,someScript)