MessageBox questions

R

Russ

I've been trying to figure out how to show a simple messagebox with an
OK button in my web client program (C#). I have looked at every
reference to JScript and MessageBox that seemed even remotely like it
could help, both in the VS help and in this NG. I found lots of
examples of people saying how easy it is and showing examples, and
examples in the help. But I have two problems:

1. All the examples show all the code in HTML, usually involving a
user action such as a button click. I need to show this message box
as a result of a return from a web service call. So the code to
invoke the message box must be in the code-behind.

2. None of the examples work!! This is the most frustrating thing I
have seen in a long time. Everyone seems to say "Try this it works",
and I try it and can't even get close! First off, when I paste some
script into my HTML, like:

<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs e) {
// Form the script to be registered at client side.
String scriptString = "<script language=JavaScript> function DoClick()
{";
scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft
..NET!</h4>'}";
scriptString += "function Page_Load(){ showMessage1.innerHTML=";
scriptString += "'<h4>RegisterStartupScript Example</h4>'}<";
scriptString += "/";
scriptString += "script>";

if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);
}
</script>

It is immediately converted to look like this:

&lt;script language="C#" runat="server"&gt;
public void Page_Load(Object sender, EventArgs e) {
// Form the script to be registered at client side.
String scriptString = "&lt;script language=JavaScript&gt; function
DoClick() {";
scriptString += "showMessage2.innerHTML='&lt;h4&gt;Welcome to
Microsoft .NET!&lt;/h4&gt;'}";
scriptString += "function Page_Load(){ showMessage1.innerHTML=";
scriptString += "'&lt;h4&gt;RegisterStartupScript
Example&lt;/h4&gt;'}&lt;";
scriptString += "/";
scriptString += "script&gt;";

if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);
}
&lt;/script&gt;

Notice that all the < and > characters have been converted to &lt; and
&gt;

Then when I manually edit the lines to correct them, the example will
not run. Usually it will compile and run, sometimes I get a server
error, but more often it seems to simply echo the code to the page.
The example above came the closest to working. When I added:

<span id="showMessage1"></span>
<br>
<input type="button" value="ClickMe" onclick="DoClick()">
<br>
<span id="showMessage2"></span>

in the form, it did put up a button, but when I click it, I get a
status line that says "error on page".

Where is the A to Z tutorial that shows exactly how to add and make
this script work in a web client application?

Even if the above example will work it seems terribly over complicated
just to put up a simple box! The validator summary can do it by just
including MessageBox="true". No script! But of course this will not
work for me since I need to tie it to a web service response.

Please help?

Thanks, Russ
 
A

Alvin Bruney [MVP]

Page.Controls.Add(new LiteralControl("<script>alert('Your message was sent
succesfully.')</script>"));


You can tell i took that from a working program right?
 
S

Scott M.

Ok, a few things....

You can format the text to be shown in a messagebox so lose all the HTML in
the innerHTML of scriptString. In fact, you don't need innerHTML at all,
just prepare a normal string with the text of your message. The example you
followed from the help is correct, but in that example, they are not making
messageboxes, they are producing text inside of a span tag.

Because you want the messagebox to be displayed in a web page, you are
limited to the messagebox provided by the Scripting Object Model of the
target browser. In other words, .NET has nothing to do with the messagebox,
it only provides a way for you to invoke the function that calls the
messagebox.

Just make the raw string, remove the innerHTML stuff and in your function
place this: alert(scriptString)

Your code behind file contains this (convert to C# if needed):

Public Sub Page_Load(Sender As Object,e As EventArgs)
Dim scriptString As String
scriptString = "<script language='JavaScript'> function DoClick()
{"
scriptString += "alert('YOUR CUSTOM MESSAGE FOR THE MESSAGEBOX TO
SHOW HERE')"
scriptString += "}</script>"

If(Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptString)
End If
End Sub


And your HTML page contains this:

<html>
<head>
</head>
<body>
<form id="myForm" runat="server">
<input type="button" value="ClickMe" onclick="DoClick()">
</form>
</body>
</html>
 
B

bruce barker

<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs e)
{
RegisterStartupScript("Startup", string.Format("<{0}>alert('Click to
continue');</{0}>","script"));
}
</script>

the funky syntax is because you can not use "<script>" in a literal in an
aspx page (though you can of course in the code behind).


-- bruce (sqlwork.com)
 
S

Scott M.

What I cannot figure out is: Why the heck aren't functions
like this in the library, instead of making people deal with the
script? I suppose it is not big deal to anyone who has been using
jscript for a while, but to someone like me who's only experience with
script was from AWK about a hundred years ago - whew!

Because what you are attempting to do is a browser based solution, not a
..NET solution. .NET simply gives you a way to tap into the browser's
functionality.

You can't produce messageboxes in a web page from the server-side code of
..NET. Think about it a second, when the .NET code is processing, it is
processing on a web server. Since a messagebox requires user input and the
user can't interact with the code that runs on the server, it's left up to
the browser to provide the GUI.
 
S

Scott M.

By the way, you probably want to rename your function to something other
than MsgBox since that is a function in the VBScript (and also in VB.NET)
language and could conflict with the interpretation of your function.
 
R

Russ

Hi Scott. I understand about where the code is executed, but still,
this solution is a function that is executed at the server, in the web
client code. Just because the script is not executed until the page
has been sent to the browser does not mean that it is not a valid
client function.

So, what would have been wrong with the library designers saying "Here
are a few functions that you can use to produce certain actions at the
browser, if executed by the client code"?

Anyway it does what I want so I am happy now. Part of my problem was
that I did not know that script could be inserted by the code - I
thought it all had to be done in the HTML part of the client. Thanks
again to all for the help and explanations.

Regards, Russ
 
A

Alvin Bruney [MVP]

In addition, you can inject script into a page using
Response.Write("<script>....
RegisterStartupScript and RegisterClientScript. Each variation gives a bit
of control on exactly where in the html resulting page the script is
injected.
 
S

Scott M.

Russ said:
Hi Scott. I understand about where the code is executed, but still,
this solution is a function that is executed at the server, in the web
client code.

Well, that's not quite true Russ. The function is not executed on the
server. All that happens on the server is that the function is delivered
(as it - not the executed results of the function, just the function's code)
to the client along with an event handler for that function.
Just because the script is not executed until the page
has been sent to the browser does not mean that it is not a valid
client function.

I'm not sure what you mean here. Of course it is a valid client function if
it runs on the client.
So, what would have been wrong with the library designers saying "Here
are a few functions that you can use to produce certain actions at the
browser, if executed by the client code"?

They did do just that, they gave us RegisterStartupScript and
control.attributes.add for adding other client event handlers.
Anyway it does what I want so I am happy now. Part of my problem was
that I did not know that script could be inserted by the code - I
thought it all had to be done in the HTML part of the client. Thanks
again to all for the help and explanations.
Glad it's working.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top