Message Box from .aspx Code-Behind

  • Thread starter Thread starter April
  • Start date Start date
A

April

Hello,
I'm fairly new to this whole ASP.NET world, so please bear with me. I have an ASP.NET .aspx page. I written most of the functionality in the code-behind .aspx.cs file. But, now I need to pop up a message box from my C# code-behind code. I don't know how to do this and haven't been very successful in finding any online help.

It's basically:

if (valid == false)
{
[code to pop up the message box here]
}

Any suggestions?

Thanks.
 
You can execute a line of code like this when you want a message box to be
displayed.
(This writes out the necessary client side javascript to your HTML page to
make the alert pop up as soon as the page is sent to their browser.)

RegisterStartupScript("startupScript", "<script
language=JavaScript>alert('This is my message.');</script>");

Here's more info:
http://msdn.microsoft.com/library/d...mWebUIPageClassRegisterStartupScriptTopic.asp

Here are a couple controls you might find to be useful:
http://www.metabuilders.com/Tools/ConfirmedButtons.aspx
http://www.jttz.com/msgbox/index.htm

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net


Hello,
I'm fairly new to this whole ASP.NET world, so please bear with me. I have an ASP.NET .aspx page. I written most of the functionality in the code-behind .aspx.cs file. But, now I need to pop up a message box from my C# code-behind code. I don't know how to do this and haven't been very successful in finding any online help.

It's basically:

if (valid == false)
{
[code to pop up the message box here]
}

Any suggestions?

Thanks.
 
Hi April,

You need to pass client-side code (javascript) to pop up the message.

if (valid == false)
{
RegisterStartupScript("myAlert","<script>alert('It\'s Not
Valid!')</script>")
}

-- Alex Papadimoulis

Hello,
I'm fairly new to this whole ASP.NET world, so please bear with me. I have
an ASP.NET .aspx page. I written most of the functionality in the
code-behind .aspx.cs file. But, now I need to pop up a message box from my
C# code-behind code. I don't know how to do this and haven't been very
successful in finding any online help.

It's basically:

if (valid == false)
{
[code to pop up the message box here]
}

Any suggestions?

Thanks.
 
Thanks for the replies! It works!

Another quick question...

Is it possible to have the message on two lines? For example, the following code doesn't work.

string strMessage = "A long message that I \n want to have on two lines.";
RegisterStartupScript("startupScript", "<script language=JavaScript>alert('" + strMessage + "');</script>");

I've tried \n, \r, \r\n, and errors is what I get with no message box. So, am I using the wrong escape character? Or, is this not allowed?

Thanks again.


Hello,
I'm fairly new to this whole ASP.NET world, so please bear with me. I have an ASP.NET .aspx page. I written most of the functionality in the code-behind .aspx.cs file. But, now I need to pop up a message box from my C# code-behind code. I don't know how to do this and haven't been very successful in finding any online help.

It's basically:

if (valid == false)
{
[code to pop up the message box here]
}

Any suggestions?

Thanks.
 
April said:
Is it possible to have the message on two lines? For example, the
following code doesn't work.

string strMessage = "A long message that I \n want to have on two
lines.";

Well...that's fine if you want to create a string with a new line character
in it, but what you want to do is pass "\n" as is to the alert function
(which is what you want interpreting the escape character, not the string
constructor). i.e. you want to create strMessage as follows :

string strMessage = @"A long message that I \n want to have on two lines";

or

string strMessage = "A long message that I \\n want to have on two lines";

HTH,
 
Back
Top