locked
Client side msgbox RRS feed

  • Question

  • User340574083 posted

    Hi

    I have my asp.net program on VB. I used msgbox whenever I want to alert the clients who logged into my page. However, when I transferred my program into my web server, the msgbox turns out to be an error.
    I understand that msgbox is the server side msgbox. I googled and found that javascript can be used to do the client side msgbox. However, I have no idea on how to integrate the javascript codes into my vb codes. Can anyone want help? I usually use the msgbox with client interactions like "Yes", or "No".

    And also, other javascript, is there vb codes for client side msgbox instead? because my java script isn't good.

    Thank you.

    Sunday, October 28, 2007 1:36 AM

Answers

  • User798903548 posted

    The easiest way is to just add this to the button which will stop the server-side event from being called if the user chooses Cancel:
    Button1.Attributes.Add("onclick", "if ( confirm('Are you sure?') != true ) return false;")

    Another way, if you have to have the results server-side is:
    In the aspx file:
     <input type="hidden" id="Hidden1" name="Hidden1" runat="server">
    That adds this to the CodeBehind:
     Protected WithEvents Hidden1 As System.Web.UI.HtmlControls.HtmlInputHidden

    Then add to the Page_Load event handler:

    Dim scriptString As String = "<script language=JavaScript> " + Environment.NewLine
    ' Store the confirm's return in the hidden control...
    scriptString += "document.getElementById('" + Hidden1.ClientID + "').value = " + Environment.NewLine
    scriptString += " confirm('Are you sure?'');" + Environment.NewLine
    ' Do a new PostBack...
    scriptString += GetPostBackEventReference(Hidden1, String.Empty) + ";" + Environment.NewLine
    scriptString += "</script>"

    RegisterStartupScript("ConfirmScript", scriptString)

    If IsPostBack AndAlso Request("__EVENTTARGET") = "Hidden1" Then
         If Hidden1.Value = "true" Then
              ' User answered OK
         Else
              ' User answered Cancel
         End If
    End If
    Hidden1.Value = String.Empty

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 28, 2007 9:09 AM
  • User798903548 posted

    Did not realize that you were using VWD. Try it like this and see what happens. Note that I executed the code on a button press as the way that I gave the code to you before, you'd get into an endless loop. BTW, I always use the obsolete method when posting samples as they are easier to post!

    <form id="Form1" method="post" runat="server">
     <asp:button id="confirmButton" runat="server" text="Confirm" OnClick="confirmButton_Click"></asp:button>
     <input type="hidden" id="confirmValue" name="confirmValue" runat="server">
    </form>

    <script runat="server">
    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     If IsPostBack AndAlso Request("__EVENTTARGET") = "confirmValue" Then
      'Me.Response.Write("confirmValue.Value: " + confirmValue.Value + "<br>")

      If confirmValue.Value = "true" Then
       ' User answered OK
      Else
       ' User answered Cancel
      End If
     End If
     confirmValue.Value = String.Empty
    End Sub

    Protected Sub confirmButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
     ' Store the confirm's return in the hidden control...
     scriptString += "document.getElementById('" + confirmValue.ClientID + "').value = " + Environment.NewLine
     scriptString += " confirm('Are you sure?');" + Environment.NewLine
     ' Do a new PostBack...
     scriptString += ClientScript.GetPostBackEventReference(confirmValue, String.Empty) + ";" + Environment.NewLine
     scriptString += "</"
     scriptString += "script>"

     ClientScript.RegisterStartupScript(Me.GetType, "ConfirmScript", scriptString)
    End Sub
    </script>

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2007 8:03 AM
  • User798903548 posted

    Well then you wouldn't need the PostBack or the value server-side, so this should work:

    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     ' Check whether the user is authorized here...
     Dim isUnauthorizedUser As Boolean = True

     If isUnauthorizedUser Then
      Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
      scriptString += "alert('You are NOT authorized to be here!');" + Environment.NewLine
      scriptString += "</"
      scriptString += "script>" + Environment.NewLine

      ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
     End If
    End Sub

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 1, 2007 7:30 AM
  • User798903548 posted

    You can't do a Response.Redirect and expect the alert to show, after all, you've left the page! Try this instead.

    If adminStatus = False And hrStatus = False Then
       Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
       scriptString += "alert('You are NOT authorized to be here!');" + Environment.NewLine
       scriptString += "window.location.href = 'Default.aspx';" + Environment.NewLine
       scriptString += "</"
       scriptString += "script>"
       ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
    End If

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 2, 2007 8:00 AM

All replies

  • User-1882386891 posted

     

    Hi ! there are many ways  through which you can achieve this :

     

    Simplest one is :

    ---------------------------------------------------------------------------------------------------------------------------------------------

    Response.Write("<script type='text/javascript'>alert('---Your message -----');</script>"); 

    ---------------------------------------------------------------------------------------------------------------------------------------------

    put this code wherever you want the message box to show..

    NOTE: it will not work if the code will be used inside a scriptmanager...

    just say if you need more clarification.. 

     

     

    Sunday, October 28, 2007 2:18 AM
  • User-204848132 posted
    you must write the following source code : clientscript.registerstartupscript(gettype(page),"","alert('---Your Message---');",true)
    Sunday, October 28, 2007 2:49 AM
  • User340574083 posted

    Hi!

    Thanks for replying! But, what if I want interactions between the clients and my server? As in in server side code is:
    If MsgBox("Do you really want this item?", MsgBoxYesNo, "Confirmation") = MsgBox.Yes Then
       TakeItem()
    Else
       Exit Sub
    End If

    How can I extract the value of the clients' response? Thanks!

    Sunday, October 28, 2007 6:54 AM
  • User798903548 posted

    The easiest way is to just add this to the button which will stop the server-side event from being called if the user chooses Cancel:
    Button1.Attributes.Add("onclick", "if ( confirm('Are you sure?') != true ) return false;")

    Another way, if you have to have the results server-side is:
    In the aspx file:
     <input type="hidden" id="Hidden1" name="Hidden1" runat="server">
    That adds this to the CodeBehind:
     Protected WithEvents Hidden1 As System.Web.UI.HtmlControls.HtmlInputHidden

    Then add to the Page_Load event handler:

    Dim scriptString As String = "<script language=JavaScript> " + Environment.NewLine
    ' Store the confirm's return in the hidden control...
    scriptString += "document.getElementById('" + Hidden1.ClientID + "').value = " + Environment.NewLine
    scriptString += " confirm('Are you sure?'');" + Environment.NewLine
    ' Do a new PostBack...
    scriptString += GetPostBackEventReference(Hidden1, String.Empty) + ";" + Environment.NewLine
    scriptString += "</script>"

    RegisterStartupScript("ConfirmScript", scriptString)

    If IsPostBack AndAlso Request("__EVENTTARGET") = "Hidden1" Then
         If Hidden1.Value = "true" Then
              ' User answered OK
         Else
              ' User answered Cancel
         End If
    End If
    Hidden1.Value = String.Empty

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 28, 2007 9:09 AM
  • User340574083 posted

    Hi!

    I've tried the vb & javascript codes, but they aren't working. For the vb codes, my program just took as if it didn't exist, while the javascript codes produced errors because I put them in a If then else condition.
    Error: Statement cannot appear within mthod body.
    How should I go about them?

    Thank so much & sorry for troubling you all~

    Monday, October 29, 2007 10:36 PM
  • User798903548 posted

    Did you try the code that I posted? I just copied it out into a page with no problems.

    NC...

    Tuesday, October 30, 2007 7:54 AM
  • User340574083 posted

    Hi NC

    Thanks for your codes and I tries yours as well. However the message box still wouldn't appear. Below are the codes that I changed because my VWD just cannot accept some codes which I don't understand why either. Please help! Thank you...

    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    ***I didn't put the Protected WithEvents Hidden1 As System.Web.UI.HtmlControls.HtmlInputHidden because Hidden1 according to my VWD says that it is already declared in this class which is the (<input type="hidden" id="Hidden1" name="Hidden1" runat="server">) I put in my asp codes.***

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim scriptString As String = "<script language=JavaScript> " + Environment.NewLine ' Store the confirm's return in the hidden control...
    scriptString += "document.getElementById('" + Hidden1.ClientID + "').value = " + Environment.NewLine
    scriptString +=
    " confirm('Are you sure?'');" + Environment.NewLine

    ' Do a new PostBack...
    scriptString += ClientScript.GetPostBackEventReference(Hidden1, String.Empty) + ";" + Environment.NewLine
    ***The error here agin says that GetPostBackEventReference(Hidden1, String.Empty) is obsolete***

    scriptString += "</"
    scriptString += "script>"
    ***I had to separate the "</script>" because my VWD keep telling me that The Statement cannot appear within method body***

    ClientScript.RegisterStartupScript(Me.GetType, "ConfirmScript", scriptString)
    ***I had to put the ClientScript. infront, again VWD said that overriding "RegisterStartupScript("ConfirmScript", scriptString)" is obsolete***

    If
    IsPostBack AndAlso Request("__EVENTTARGET") = "Hidden1" Then
       
    If Hidden1.Value = "true" Then
              
    ' User answered OK
       
    Else
               
    ' User answered Cancel
       
    End If
    End If

    Hidden1.Value = String.Empty

    End Sub

     

    Tuesday, October 30, 2007 10:22 PM
  • User340574083 posted

    Hi

    I just realised something after just passing by a comment. Is it true that ASP.NET web pages with ".aspx" file names cannot use javascript?

    Or should I use XML dialog box instead? (was thinking web.config was written in XML I guess...?)

    Sorry, I realise that I still don't understand ASP.NET very much even though I program my enitre project on ASP.NET.

    Please advice. Thank you very much!

    Tuesday, October 30, 2007 11:00 PM
  • User798903548 posted

    Did not realize that you were using VWD. Try it like this and see what happens. Note that I executed the code on a button press as the way that I gave the code to you before, you'd get into an endless loop. BTW, I always use the obsolete method when posting samples as they are easier to post!

    <form id="Form1" method="post" runat="server">
     <asp:button id="confirmButton" runat="server" text="Confirm" OnClick="confirmButton_Click"></asp:button>
     <input type="hidden" id="confirmValue" name="confirmValue" runat="server">
    </form>

    <script runat="server">
    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     If IsPostBack AndAlso Request("__EVENTTARGET") = "confirmValue" Then
      'Me.Response.Write("confirmValue.Value: " + confirmValue.Value + "<br>")

      If confirmValue.Value = "true" Then
       ' User answered OK
      Else
       ' User answered Cancel
      End If
     End If
     confirmValue.Value = String.Empty
    End Sub

    Protected Sub confirmButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
     ' Store the confirm's return in the hidden control...
     scriptString += "document.getElementById('" + confirmValue.ClientID + "').value = " + Environment.NewLine
     scriptString += " confirm('Are you sure?');" + Environment.NewLine
     ' Do a new PostBack...
     scriptString += ClientScript.GetPostBackEventReference(confirmValue, String.Empty) + ";" + Environment.NewLine
     scriptString += "</"
     scriptString += "script>"

     ClientScript.RegisterStartupScript(Me.GetType, "ConfirmScript", scriptString)
    End Sub
    </script>

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2007 8:03 AM
  • User340574083 posted

    Hi NC!

    Thank you! This works! However I have to change "Protected Sub controlExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)" to "confirmButton_Click".

    But just another 1 more Q, because this uses the click of the button control, is there that I can use without the click of any button control?
    Initially I wanted to use it for if an unauthorised user logs into the system, I want a message box to appear to tell the user that "Unauthorised Access Denied."

    How am I able to do this?

    Thank you once again.

    Wednesday, October 31, 2007 9:55 PM
  • User798903548 posted

    Well then you wouldn't need the PostBack or the value server-side, so this should work:

    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     ' Check whether the user is authorized here...
     Dim isUnauthorizedUser As Boolean = True

     If isUnauthorizedUser Then
      Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
      scriptString += "alert('You are NOT authorized to be here!');" + Environment.NewLine
      scriptString += "</"
      scriptString += "script>" + Environment.NewLine

      ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
     End If
    End Sub

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 1, 2007 7:30 AM
  • User340574083 posted

    Hi

    Thank you for your continuous help. However, the codes this time didn't work. Is it because of the start up script?
    I don't really understand how it works there. Below are my codes, please advice. Thank you very much.

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim adminStatus As Boolean
    Dim hrStatus As Boolean

    adminStatus = Roles.IsUserInRole("Administrator")
    hrStatus = Roles.IsUserInRole(
    "Human Resource")

    If adminStatus = False And hrStatus = False Then
       Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
       scriptString +=
    "alert('You are NOT authorized to be here!');" + Environment.NewLine
       scriptString +=
    "</"
      
    scriptString += "script>"
      
    ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
       Response.Redirect(
    "~/Default.aspx")
    End If
    End Sub

    </script>

    <head runat="server">
    <title>Choose to view or add accounts</title>
    </
    head>

    <body bgcolor="#99ccff">
    <form id="form1" runat="server">

    </form>
    </
    body>
    </
    html>

    Thursday, November 1, 2007 9:25 PM
  • User798903548 posted

    You can't do a Response.Redirect and expect the alert to show, after all, you've left the page! Try this instead.

    If adminStatus = False And hrStatus = False Then
       Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
       scriptString += "alert('You are NOT authorized to be here!');" + Environment.NewLine
       scriptString += "window.location.href = 'Default.aspx';" + Environment.NewLine
       scriptString += "</"
       scriptString += "script>"
       ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
    End If

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 2, 2007 8:00 AM
  • User340574083 posted

    Thank you so much for your continuous help!

    Sunday, November 4, 2007 7:51 PM
  • User340574083 posted

    Hi

    Just wanna to ask you another question. I realised that in ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)

    Is there a format? Or can I put in any string? Beacuse in your earlier sample, you used ComfirmScript instead.

    Thank you

    Sunday, November 4, 2007 9:41 PM
  • User798903548 posted

    That is a key so that the same script is not added more than once.

    NC...

     

    Monday, November 5, 2007 7:36 AM
  • User340574083 posted

    Hi

    Thanks for replying. Just another one more, what if the other web pages uses the same function like UpdatedScript?

    Thank you so much!

    Monday, November 5, 2007 7:50 PM
  • User798903548 posted

    Other web pages won't matter. You can use the script in every page for that matter. The key just prevents multiple insertion in the same page.

    NC...

    Tuesday, November 6, 2007 7:37 AM
  • User863330749 posted

    For MessageBox i created a webusercontrol. i treid alot but iam not getting that like windows based messagebox.

    This is Prashanth 

    For MessageBox i created a webusercontrol. i treid alot but iam not getting that like windows based messagebox.

    -> I can use this messagebox for Error display and required buttons to use for end-user like YES,NO,CANCEL,ABORT,OK..............

    Prashanth

    Tuesday, November 20, 2007 3:25 PM
  • User340574083 posted

    Hi, I think that you want the message box to return you something right? Below are the codes that someone in this thread have helped me before, it works, but it does a postback to get your answers. 

     

    Protected Sub confirmButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
     ' Store the confirm's return in the hidden control...
     scriptString += "document.getElementById('" + confirmValue.ClientID + "').value = " + Environment.NewLine
     scriptString += " confirm('Are you sure?');" + Environment.NewLine
     ' Do a new PostBack...
     scriptString += ClientScript.GetPostBackEventReference(confirmValue, String.Empty) + ";" + Environment.NewLine
     scriptString += "</"
     scriptString += "script>"

     ClientScript.RegisterStartupScript(Me.GetType, "ConfirmScript", scriptString)
    End Sub
    </script>

    Tuesday, November 20, 2007 7:45 PM
  • User-370989946 posted

    You can't do a Response.Redirect and expect the alert to show, after all, you've left the page! Try this instead.

    If adminStatus = False And hrStatus = False Then
       Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine
       scriptString += "alert('You are NOT authorized to be here!');" + Environment.NewLine
       scriptString += "window.location.href = 'Default.aspx';" + Environment.NewLine
       scriptString += "</"
       scriptString += "script>"
       ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
    End If

     

    Thanks so much for your solution, NC01.

    I had tried just about everything and they either didn't work at all or messed up styles in the master pages, but this one seems to work perfectly.

    Gretchen

     

    Sunday, January 27, 2008 8:51 AM
  • User798903548 posted

    No problem Gretchen glad to be of help.

    NC...

     

    Sunday, January 27, 2008 10:53 AM
  • User670892346 posted

     Guys, this thing is just not working for me. My code just skips it like it didn't exist. I remember i was trying to add an onclick event code to a button and it want work either.

     Help!!!!!!!!!!

     

    Thursday, March 27, 2008 12:28 PM
  • User340574083 posted

    Hi... Could you post your codes in so that we can understand your problem better? Thx!

    Thursday, March 27, 2008 12:53 PM
  • User670892346 posted

     Hi. I have tried try different approaches and it all doesn't work.

     

    1    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlreadyEdited", scriptString, True)
    2
    3 ClientScript.RegisterStartupScript(Me.GetType, "UnauthorizedUserScript", scriptString)
    4
    5 Response.Write("<script type='text/javascript'>alert('--Message-- ');</script>")
    6
    7
      I have tried all this but it just skips me code and does not display anything.
    Thursday, March 27, 2008 1:24 PM
  • User798903548 posted

    1. Use the ScriptManager object for AJAX calls.
    2. Use the ClientScript object for all others.
    3. Never use Response.Write except in debugging.

    Otherwise it is very hard to tell what the problem is from what you have posted. What does scriptString look like? Are you sure that the script is being outputed (do a View Source in your browser and look for it)? Is something else happening that is short circuiting the JavaScript calls?

    NC...

    Friday, March 28, 2008 1:51 PM
  • User670892346 posted

     Thanks for your reply. this is the whole code i am using.  

     
    1     Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine"
    2 scriptString += "alert('--Message--');" + Environment.NewLine"
    3 scriptString += "
    window.location.href = 'EventsDisplay.aspx';" + Environment.NewLine"
    4 scriptString += "</"
    5 scriptString += "script>"
    6
    7
    8 ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlreadyEdited", scriptString, True)
    9 Response.Redirect("EventsDisplay.aspx")
    10
    11
     
     The application skips line 8 and nothing happens. I have a scriptmanager on the page. what else do i need to do to make this work?. I new to web development. Please help.
     
    Monday, March 31, 2008 4:02 PM
  • User798903548 posted

    You are doing a Response.Redirect, which means that you are leaving the page. The page never gets rendered so your script will never get executed. Read some of the earlier posts, because I believe something like that is what started this thread.

    NC...

     

    Tuesday, April 1, 2008 7:50 AM
  • User670892346 posted

     Thanks very much!! U are a savior.

    Tuesday, April 1, 2008 9:43 AM
  • User635636092 posted

    try this code

     

    Dim S As String = "<script language='javascript'>alert('You Can not Edit Till All Above Item Send ');</script>"

    ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "onclick", S, False)

    Wednesday, April 2, 2008 8:42 AM
  • User-238792484 posted

    I know this thread is old but its worth a shot. I have this code:

    ClientScript.RegisterStartupScript(Me.GetType, "Invalid Date Range!", "alert('""From"" date must be before the ""To"" date.');", True)

    the message box works and once the user corrects the date and hits the submit button again the code works fine and forwards the user to the next page, however, when the user hits back to return to the page the message box shows up again. anyone know a way to stop this?

    Monday, April 9, 2012 2:26 PM