User Message

  • Thread starter Thread starter szabelin
  • Start date Start date
S

szabelin

Hi I want to pop up a little window (sort of a message
box), so I call MessageBoxA function during postback, and
I get "error on page" icon. What's the way to popup a new
window? Thanks!


public static void MessageBoxA(string message)
{
HttpContext.Current.Response.Write("<script
language='javascript'>");
string writestring = "myWindow = window.open(\"\",
\"tinyWindow\", 'toolbar,width=150,height=100');"+
"myWindow.document.write(\"" + message + "\");" +
"myWindow.document.bgColor=\"lightblue\";" +
"myWindow.document.close(); ";

HttpContext.Current.Response.Write(writestring);
HttpContext.Current.Response.Write("</script>");

}
 
You either have to create a new browser window, or use the javascript alert
method

See below for some examples
--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------


<html>
<head>
<script language="VB" runat="server">

Sub Page_Load( sender as Object,e as EventArgs)

'Form the script that is to be registered at client side.
Dim scriptString as String = "<script language=JavaScript> function
DoClick() {"
ScriptString +=
"window.open('http://www.mvps.org','MyWindow','width=500,height=500');}<"
scriptString += "/"
scriptString += "script>"

If(Not IsClientScriptBlockRegistered("clientScript"))
RegisterClientScriptBlock("clientScript", scriptString)
End If
End Sub
</script>
</head>
<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">
<input type="button" value="ClickMe" onclick="DoClick()">
</form>
</body>
</html>

<html>
<head>
<script language="VB" runat="server">

Sub button_onClick( sender as Object,e as EventArgs)
Response.Write("Postback as requested!!!")
End Sub


</script>
</head>

<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">

<input type="button" value="OK" onserverclick="button_onClick"
onclick="if(!confirm('You wanna do the postback?')){return false;}"
runat="server">


</form>
</body>
</html>
 
Hello John, thanks for reply. So I am going to
RegisterClientScriptBlock - but how do I simulate a call
to fucntion "DoClick" from your example - my popup is not
a result of a button click, rather it depends on some
processing I do first in response to a postback

Thank you
S
 
you should avoid this approach, as pop ad blockers will prevent your window
from appearing.


-- bruce (sqlwork.com)
 
They may well do that..mine certainly does, but they dont tend to stop
Javacript alerts easily and there are few options for popping to the screen.

All the button click example does is call a local (client) javascript
function, so you could call this in HTML body onload event, or wherever you
choose. You will still need to send the script as part of your page
response and call it.



--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------
 
I am sorry - again - how do you call a popup (javascript
fuction) from the C# code in asp.net application. I got
the registration part. Thanks John.
 
Your javascript is not correct, try this.

public static void MessageBoxA(string message)
{
HttpContext.Current.Response.Write("<script language='javascript'>");
string writestring =
"myWindow = window.open'','tinyWindow','toolbar,width=150,height=100');"+
"myWindow.document.write('" + message + "');" +
"myWindow.document.bgColor='lightblue';" +
"myWindow.document.close(); ";

HttpContext.Current.Response.Write(writestring);
HttpContext.Current.Response.Write("</script>");

}

Hope this helps.
Ram.
 
you cant call it from C# - you cant popup windows server side, which is what
would happen if you could do this in C#. You have to do this in the browser
as part of the response, and get the browser to do whatever it is you need
using javascript.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------
 
Back
Top