Pop Up Window from .cs file

S

SJ

Is it possible to pop-up a window from the code-behind
aspx.cs file on an OnClick event?
If so, how?
I've tried something like the following, but it doesnt seem to work.

private void MyButton_Click(object sender, System.Web.UI.ImageClickEventArgs
e)
{
Response.Write("<script>window.open('mypopUp_dialog.aspx');<script>");
}

I don't want to use in-line javascript code or use a <script> tag within my
aspx file
itself.
 
B

Bruce Barker

it would work if your turned off you browsers popup blocking. to not be
blocked, the win.open() must be from a browser client onclick event, not a
postback.

-- bruce (sqlwork.com)
 
M

matija.rovan

insert this to your code:

string popupScript =
"<script language='javascript'>" +
"window.open('PopupPage.aspx','Popup', 'width=700, height=600, menubar=no, resizable=no')" +
"</script>";

Page.RegisterStartupScript("PopupScript", popupScript);
 
D

Dave Hagerich

The problem is that you forgot the forward slash in the close script tag.
That causes the code to throw an error and not execute. You need to make
the last tag </script>.
 
G

Guest

//add this class and then you can call MessageBox.Show("string");


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace InnisMaggioreGroup
{
/// <summary>
/// Summary description for MessageBox.
/// </summary>
public class MessageBox
{
private static Hashtable m_executingPages = new Hashtable();

private MessageBox(){}

public static void Show( string sMessage )
{
// If this is the first time a page has called this method then
if( !m_executingPages.Contains( HttpContext.Current.Handler ) )
{
// Attempt to cast HttpHandler as a Page.
Page executingPage = HttpContext.Current.Handler as Page;

if( executingPage != null )
{
// Create a Queue to hold one or more messages.
Queue messageQueue = new Queue();

// Add our message to the Queue
messageQueue.Enqueue( sMessage );

// Add our message queue to the hash table. Use our page
reference
// (IHttpHandler) as the key.
m_executingPages.Add( HttpContext.Current.Handler,
messageQueue );

// Wire up Unload event so that we can inject
// some <B style="COLOR: black; BACKGROUND-COLOR:
#99ff99">JavaScript</B> for the alerts.
executingPage.Unload += new EventHandler(
ExecutingPage_Unload );
}
}
else
{
// If were here then the method has allready been
// called from the executing Page.
// We have allready created a message queue and stored a
// reference to it in our hastable.
Queue queue = (Queue) m_executingPages[
HttpContext.Current.Handler ];

// Add our message to the Queue
queue.Enqueue( sMessage );
}
}


// Our page has finished rendering so lets output the
// <B style="COLOR: black; BACKGROUND-COLOR: #99ff99">JavaScript</B>
to produce the <B style="COLOR: black; BACKGROUND-COLOR: #ff9999">alert's</B>
private static void ExecutingPage_Unload(object sender, EventArgs e)
{
// Get our message queue from the hashtable
Queue queue = (Queue) m_executingPages[
HttpContext.Current.Handler ];

if( queue != null )
{
StringBuilder sb = new StringBuilder();

// How many messages have been registered?
int iMsgCount = queue.Count;

// Use StringBuilder to build up our client slide <B
style="COLOR: black; BACKGROUND-COLOR: #99ff99">JavaScript</B>.
sb.Append( "<script language='javascript'>" );

// Loop round registered messages
string sMsg;
while( iMsgCount-- > 0 )
{
sMsg = (string) queue.Dequeue();
sMsg = sMsg.Replace( "\n", "\\n" );
sMsg = sMsg.Replace( "\"", "'" );
sb.Append( @"alert( """ + sMsg + @""" );" );
}

// Close our JS
sb.Append( @"</script>" );

// Were done, so remove our page reference from the hashtable
m_executingPages.Remove( HttpContext.Current.Handler );

// Write the <B style="COLOR: black; BACKGROUND-COLOR:
#99ff99">JavaScript</B> to the end of the response stream.
HttpContext.Current.Response.Write( sb.ToString() );
}
}
}
}
 

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

Top