Hi friends..

I am trying to display message box using server-side code. I have written code in buttons click event and Page Load Event. It is as follows :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Text = "I Enjoy ASP.NET"

        Dim myscript As String = "alert(document.getElementById('TextBox1').value);"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "myscript", myscript, True)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strMessage As String
        strMessage = "Hi, You clicked ClickMe button"
        Dim strScript As String = "<script language=JavaScript>"
        strScript += "alert('" & strMessage & "');"
        strScript += "</script>"

        If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "clientScript", strScript)
        End If
    End Sub

If button is not in UPDATE PANEL, then both alert are displayed normally (i.e. during Page Load and Button Click)

But when I put the button inside UPDATE PANEL , alert dosn't work in Button Click. Here only Page_Load alert is displayed
In case of UPDATE PANEL is also tried
Me.UpdatePane1.GetType()
but in vain..!!!
Any ideas??

Dani AI

Generated

Short diagnosis: the script you register with the page-level client-script APIs is sent only on a full page render. An UpdatePanel click performs an asynchronous partial postback, so scripts registered with the page’s older client-script manager won’t be included in the partial response — that’s why your Page_Load alert appears but the button-click alert does not. (learn.microsoft.com)

What to do (concise): ’s direction is correct — register the JavaScript through the ASP.NET AJAX ScriptManager so it is emitted with the partial-postback response. Use the ScriptManager registration methods (startup vs client-script-block) and give each registration a stable or unique key; ’s idea of using a GUID for the key avoids accidental duplicates. The ScriptManager API is designed for UpdatePanel/partial-postback scenarios. (learn.microsoft.com)

Practical VB example (safe string handling and correct client id):

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim message As String = "Hi, you clicked the button"
    Dim safe As String = System.Web.HttpUtility.JavaScriptStringEncode(message)
    Dim script As String = String.Format("alert('{0}');", safe)
    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "btnClickAlert", script, True)
End Sub

Use Control.ClientID whenever you reference server controls from the emitted script (naming containers can change the rendered id). Also ensure a ScriptManager control exists on the page and that the UpdatePanel triggers are configured correctly. If the script touches freshly replaced DOM nodes, reinitialize handlers (or run code on the PageRequestManager endRequest event) after the partial update. (learn.microsoft.com)

References: ScriptManager.RegisterStartupScript and HttpUtility.JavaScriptStringEncode docs linked above explain the APIs and safe string encoding for embedding server values into client scripts.

/forums/clientscriptManager class does not work inside UpdatePanel. Use the ScriptManager class instead.

try the folllowing code it will dafinately work

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "alert('" "')", true);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.