0

Here is what I have tried.

ASPX File:

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>
                 <asp:Button OnClick="btnOK1_Click" ID="btnOK1" runat="server" Text="Save" />
            </ContentTemplate>

        </asp:UpdatePanel>

ASPX.VB:

        Protected Sub btnOK1_Click(sender As Object, e As EventArgs)
            Dim alertMsg As String
            Dim alertScript As String
            alertMsg = String.Format("You Selected {0}", "My Message")
            alertScript = String.Format("<script type= text/javascript>alert('{0}');</script>", alertMsg)
            ClientScript.RegisterStartupScript(Me.[GetType](), "Alert", alertScript)
        End Sub

I tried to get an alert box with a message when clicked a button (Save) from backend. I tried with the solutions from the StackOverflow. But, the codes does not work for me to get an javascript alert box. The event triggers fine and runs through the code. Anyone help me with this?

Giri Dharan
  • 161
  • 4
  • 28

2 Answers2

1
replace your code with these

Protected Sub btnOK1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim alertMsg As String
    Dim alertScript As String
    alertMsg = String.Format("You Selected {0}", "My Message")
    alertScript = String.Format("<script type='text/javascript'>alert('{0}');</script>", alertMsg)
    ScriptManager.RegisterStartupScript(Page, Page.GetType, "Alert", alertScript, False)

End Sub
Learner
  • 59
  • 1
  • 7
1

The problem seems to lie with the fact that you are using ClientScript rather than ScriptManager.

ClientScript is used for Synchronous postbacks, whereas ScriptManager for Asycronous.

See this question for more details.

Community
  • 1
  • 1
Pudd
  • 449
  • 3
  • 13