1

I want to display my validation error messages in the MessageBox. I have Four TextBoxes and One Button control. When I click the Button Control, the TextBoxes without the text to be shown in the MessageBox. I have almost done this, but the problem is When I click the Button, the MessageBox is opened as a minimized window. So it is difficult for the end user to realize. I want to display the MessageBox to the user when button clicks.

Here is my code, In the Button Click Event

ErrorMsg="";

if (TextBox1.Text == "")
{
    ErrorMsg += "Name is required!";
    ErrorMsg += "\n";
}
if (TextBox2.Text == "")
{
    ErrorMsg += "Address is required!";
    ErrorMsg += "\n";
}
if (TextBox3.Text == "")
{
    ErrorMsg += "Phone No. is required!";
    ErrorMsg += "\n";
}
if (TextBox4.Text == "")
{
    ErrorMsg += "City is required!";
    ErrorMsg += "\n";
}
if (ErrorMsg.Length == 0)
{
     //Some Code
}
else
{
    MessageBox.Show(ErrorMsg, "Existing Address", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Thanks in advance...

4 Answers 4

5

You cannot use the Windows MessageBox in an ASP.NET application. It just doesn't make sense. An ASP.NET spits HTML/javascript/CSS so that's what you should be using to inform the user that something went wrong. So for example in an ASP.NET application you could use the RegisterStartupScript method to inject javascript into the page which will execute when the page is loaded and use the alert function:

ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('oops');", true);

Other more conventional techniques to perform validation in an ASP.NET application involve using the validation controls.

Sign up to request clarification or add additional context in comments.

4 Comments

So far I have used Validation Controls. But the user asked me to display as a MessageBox.
@thevan, then you will have to explain the user that ASP.NET and WinForms is not the same thing and what he is asking just doesn't make sense as an ASP.NET application runs on the server. Good luck with this explanation as you seem to have a client with interesting expectations :-)
I would suspect that what the user wants is an alert not a MessageBox but being a user is unaware of the difference since, from a user experience perspective they aren't that different
i have tested and it works (windows message box in asp.net)...Can you explain why we cant use this?? Please explain as am a beginer :)
1

we can use windows Message Box Even in ASP.Net WEB Application Needs to Perform Steps Below

  1. Open Solution Explorer in Visual Studio and Right Click on Reference > Add Refrence
  2. Search system.windows and system .windows.form assemblies and add them
  3. then in your .aspx form where you want to use message box add assembly using system.windows and system.windows.form then you can use all functionalities like windows application in your web page

Comments

0

Create a function like

private void MessageBoxShow(Page page, string message)
{
    Literal ltr = new Literal();
    ltr.Text = @"<script type='text/javascript'> alert('" + message + "') </script>";
    page.Controls.Add(ltr);
}

and call it from any .aspx page like

MessageBoxShow(this,message);

the important thing is, if you use UserControl, then you need to change function parameter Page to UserControl.

2 Comments

You should use RegisterClientScriptBlock() rather then Controls.Add()
@abatishchev actually Im not very good at asp.net. I posted this, because Im using it and fine for me :) If you say you should use, I say: Yes I should :)
0

Why don't you just use the Ajax control toolkit?

You can then tweak the content of the pop up and I believe it's less intrusive than the windows style pop up message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.