MessageBox from asp.net and c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    MessageBox from asp.net and c#

    Hi there.

    Could you advise on the use of a MessageBox functionality within an aspx.cs page please?

    I've had a look over the web and a lot of the ideas / solutions don't actually work for me.

    What I need is a MessageBox with Yes / No confirmation, so I can act on a User's response accordingly.

    Thank you.

    M :o)
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Look into using the JavaScript "confirm" feature.

    For example:
    Code:
    function ConfirmWhatToDo()
    {
      var answer = confirm("Would you like to have your question answered by Bytes?");
      if (answer)
      {
        alert("Good Idea!")
        window.location = "http://www.bytes.com/";
      }
      else
      {
        alert("You're crazy, Bytes has all the answers!")
      }
    }

    Comment

    • E11esar
      New Member
      • Nov 2008
      • 132

      #3
      Javascript

      Thank you for that.

      How do I call that function from within C# please?

      Thank you.

      M :o)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You don't call it from your C# code.
        You apply it to your buttons or links.

        For example, you'd somehow add the following JavaScript to the page (whether it be copying it into the page, adding it as an external link, or dynamically generating it)

        [code=javascript]
        function ConfirmWhatToDo ()
        {
        var answer = confirm("Do you Confirm?");
        return answer;
        }[/code]

        Then you would apply it to your control by the control's the "onclick" event to call it.

        For example, say you had the following ASP Button control on your page:

        [code=asp]<asp:Button ID="myButton" runat="server" Text="Click Me" />[/code]

        In your C# code you would do the following:

        Code:
        myButton.Attributes.Add("onclick","return ConfirmWhatToDo();")
        Note that the "onclick" returns the value from the function "ConfirmWhatToD o().
        If the function returns false, the button will not submit to the server; otherwise everything continues like normal. You don't even really need to use a function, you could just do:

        Code:
        myButton.Attributes.Add("onclick","return confirm("Do you Confirm?");")

        Check out this article. It gives an example of what I'm talking about.


        -Frinny

        Comment

        • sangam56
          New Member
          • Nov 2007
          • 68

          #5
          Hi E11esar,
          There certainly exists work around for the windows forms like message box in asp.net web application. And it is much simpler! Check out this solution: Windows Forms Like MessageBox in asp.net web page

          Hope this helps!

          Comment

          • jim bradshaw
            New Member
            • Feb 2009
            • 4

            #6
            do this if you only want to display info

            response.write "<script language='javas cript'>alert('T his is my message')</script>"
            Last edited by jim bradshaw; Feb 12 '09, 11:06 PM. Reason: included extranious code

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Mmmm....respons e.write is a "no - no" in ASP.NET and should not be used.
              There are many other ways around it.

              Comment

              • Vkas
                New Member
                • Feb 2009
                • 78

                #8
                response .write works!!!!
                their are many other ways !!!1
                but are a little complex!!

                Simply use this code in your aspx.cs file

                Response.Write( "<script language='javas cript'>alert( ' my message' )</script>")

                Comment

                • Vkas
                  New Member
                  • Feb 2009
                  • 78

                  #9
                  search them u will get some guide

                  For the codes I've seen three is three way to invoke javascript in ASP
                  ..NET:
                  1) Using RegisterClientS cript();
                  2) Using RegisterStartup Script();
                  3) Using HttpContext.Cur rent.Respose.Wr ite();

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Response.Write should not be used in your server side C# or VB.NET code.
                    The reason is because it writes the content "somewhere" in the HTML code.
                    Usually it happens above the <head> section of the the page. This makes the page invalid and could cause problems for many browsers trying to render the invalid content.

                    So, if you use:

                    Code:
                    Response.Write("<script language='javascript'>alert( ' my message' )</script>")
                    This could appear as such in your html page:
                    Code:
                    <script language='javascript'>alert( ' my message' )</script>
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml" >
                    <head>
                        <title>
                        </title>
                    </head>
                    <body>
                    </body>
                    When it should be as such:
                    Code:
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml" >
                    <head>
                        <title>
                        </title>
                         <script language='javascript'>alert( ' my message' )</script>
                    </head>
                    <body>
                    </body>
                    Or as such:
                    Code:
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml" >
                    <head>
                        <title>
                        </title>
                        
                    </head>
                    <body>
                          <script language='javascript'>alert( ' my message' )</script>
                    </body>

                    Response.Write has its place though.
                    You can call it in your ASP code to print content into the page.

                    For example, in your ASP code, you may want to write some JavaScript that contains dynamic content....

                    Say, for instance, you want to display the content of a TextBox when a button is clicked. You need to access the ClientID of the TextBox in the JavaScript code that displays the content. You can use the Response.Write( ) method in the ASP code to write the ClientID of the TextBox directly in the JavaScript:


                    Code:
                    <%@ Page Language="vb" AutoEventWireup="false"  CodeBehind="MyPage.aspx.vb" Inherits="MyNamespace.MyPage" 
                        Culture="en-US" UICulture="auto" %>
                    
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml" >
                    <head Id="Head1" runat="server">
                        <title></title>      
                    </head>
                    <body>
                    
                         <asp:TextBox ID="myTextBox" runat="server" ></asp:TextBox>
                         <asp:Button ID="myButton" runat="server" Text="Click Me"  OnClientClick="ShowTextBoxContent();"/>
                    
                          <script type="text/javascript">
                            <!--
                                 function ShowTextBoxContent()
                                 {  
                                     alert(document.getElementById('<% Response.Write(myTextBox.ClientID)%>').value);
                                 }
                            --> 
                          </code>
                    </body>

                    In the above example, the content is written into the appropriate place in the page.

                    The page's content remains valid, the JavaScript is dynamically created, and everything works properly. The problem with using Response.Write in the server side code is that it will write the content somewhere in the page and most of the time it is not in the correct place. This creates invalid HTML and can really mess up in some browsers because the browser is left to guess what you want.

                    So, in a way you are correct, you can use Response.Write to help you create dynamic JavaScript, just not in your server side code.

                    Comment

                    • todashah
                      New Member
                      • Feb 2008
                      • 26

                      #11
                      If u don't want to use java scripts then other option is also available in .net framework 2.0.

                      Select Website Menu from that select Add References... select .NET tab
                      & then select System.Windows. Forms then click ok button.

                      Now if u use Messagebox then write following syntax in click event of button

                      System.Windows. Forms.MessageBo x.Show("Hello", "Message", System.Windows. Forms.MessageBo xButtons.YesNo) ;

                      I think this guideline is useful to u.

                      Bye..Enjoy Coding..

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        You should stay away from using System.Windows. Forms controls in an asp.net application.

                        If you do use them, your application will only work in Internet Explorer because in order to use System.Windows. Forms controls the page must use ActiveX to work.

                        If you can avoid using ActiveX in your web pages you should do so.
                        It is considered suspicious and not trusted because ActiveX uses the clients Operating System in order to work. ActiveX gives web pages access to the client's Operating System...so you can imagine why it's regarded as unsafe.

                        I strongly recommend sticking with regular web controls in your web applications.
                        If your looking to enhance user experience with message boxes then JavaScript is the way to go.

                        There is a free Ajax Toolkit that provides a lot of very useful tools for web development.

                        Comment

                        Working...