views:

730

answers:

4

Hi,

How to use Java script alert msg in code behind? This message will be display after save the data.

Here is my code,

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

but is not working. give me an idea.

A: 

Maybe you should echo AlertMsg.

Lex
+2  A: 

You should register this script to your page, so this script will be sent to the client.

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

A better way to do this is to have this as a utility method in a common class:

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

This can be used in a page like this:

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

Your code looks so simpler. Hope this helps!

Vijay
A: 

Dim someScript As String = "" someScript = "alert('Your Leave request has been registered');" Page.ClientScript.RegisterStartupScript(Me.GetType(), "onload", someScript)

suresh