By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,702 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,702 IT Pros & Developers. It's quick & easy.

Message Box from .aspx Code-Behind

P: n/a
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.

Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/de...criptTopic.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
"April" <Ap***@April.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #2

P: n/a
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

"April" <Ap***@April.com> wrote in message
news:ug**************@tk2msftngp13.phx.gbl...
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.
Nov 18 '05 #3

P: n/a
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.
"April" <Ap***@April.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #4

P: n/a
April wrote:
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,
--
Jo Inferis
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.