locked
where am doing wrong RRS feed

  • Question

  • Hello Folks,
    Please suggest me
    am getting error 
    this  MessageBox does n't exist in the current context
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                HappyBirthday birthdayMessage = new HappyBirthday();
                MessageBox.Show(birthdayMessage.getMessage());
                
            }
        }
    }

    coolboy
    Monday, September 12, 2011 6:13 AM

Answers

  • You are working on Web application. If you want to use "MessageBox", you need to add reference to "System.Windows.Forms" assembly.

    But this is not the best way to deal with it. Instead show this message using Javascript "alert".

    Hope this helps.


    Pravin Arote, MCTS - Web Technologies. If reply answers your question, Please mark as Answer :)
    • Proposed as answer by Pravin S Arote Monday, September 12, 2011 6:18 AM
    • Marked as answer by Martin_Xie Monday, September 19, 2011 8:53 AM
    Monday, September 12, 2011 6:18 AM
  • hi pravin

    Can you show how can i add the javascript to my code

     

    Thanks all for your help and suggestions.

    Hi coolboy,

    The code sample is as below.

    HappyBirthday birthdayMessage = new HappyBirthday();

    Response.Write("<Script language='javascript'>alert('" +  birthdayMessage.getMessage() + "');</Script>");


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Martin_Xie Monday, September 19, 2011 8:53 AM
    Wednesday, September 14, 2011 1:54 AM

All replies

  • You are working on Web application. If you want to use "MessageBox", you need to add reference to "System.Windows.Forms" assembly.

    But this is not the best way to deal with it. Instead show this message using Javascript "alert".

    Hope this helps.


    Pravin Arote, MCTS - Web Technologies. If reply answers your question, Please mark as Answer :)
    • Proposed as answer by Pravin S Arote Monday, September 12, 2011 6:18 AM
    • Marked as answer by Martin_Xie Monday, September 19, 2011 8:53 AM
    Monday, September 12, 2011 6:18 AM
  • MessageBox defined in System.Windows.Forms, you are trying to use it in web application. If you wan to display a message to the user, you have to use java script alert (or vbscript), please note that your code executes at the sever not at the client.
    Monday, September 12, 2011 6:22 AM
  • hi pravin

    Can you show how can i add the javascript to my code

     

    Many thanks....


    coolboy
    Monday, September 12, 2011 6:28 AM
  • take a look  at http://www.codeproject.com/KB/aspnet/ClientServer.aspx

    and

    http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

    and adding the reference of System.Windows.Forms will not work because it is a web app and the UI will be on client side so how can a message box object be created on the server side and displayed on the client side.... Not Possible

     


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

    Surender Singh Bhadauria

     


    Monday, September 12, 2011 6:47 AM
  • You can add using below below script.

    <% Page.ClientScript.RegisterStartupScript(this, "somekey", "alert('message from server'), true"); %>

    Monday, September 12, 2011 8:04 AM
  • hi pravin

    Can you show how can i add the javascript to my code

     

    Thanks all for your help and suggestions.

    Hi coolboy,

    The code sample is as below.

    HappyBirthday birthdayMessage = new HappyBirthday();

    Response.Write("<Script language='javascript'>alert('" +  birthdayMessage.getMessage() + "');</Script>");


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Martin_Xie Monday, September 19, 2011 8:53 AM
    Wednesday, September 14, 2011 1:54 AM