This is no a good method , you only want to Open a popup window on client ,why postback to server ?
Maybe I should've been more specific. The original poster of the question actually asked for server side processing (putting values in Session) and instead of doing a response.redirect, open up a new window. So in this case it makes sense to do it like I
did in my post.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
That way the code is all in one place (in the code behind file) instead of having part of it in each file. Removing the "return false;" part will allow the button to continue with its postback.
Expanding on this, you can even do fun things like this:
Button1.Attributes.Add("onclick", "javascript: return window.confirm('Are you sure you REALLY want to click that button?');");
Is it somehow possible to do this without doing a PostBack (RegisterStartUpScript) requires a postback to work.
Why I am asking this is because I want to run a javascript function after doing some server side processing.
Thanks.
- Saurabh
The difficult gets done quickly.
Its only the impossible that takes some time.
Is it somehow possible to do this without doing a PostBack (RegisterStartUpScript) requires a postback to work.
Why I am asking this is because I want to run a javascript function after doing some server side processing.
Hi,
if you're doing some server side processing first that also does need a postback or it should be that you're doing it in the page_load so the javascript gets loaded the first time you make a request to the webform.
The original code was indeed adding the javascript after a button's Click event was handled, hence the postback.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
>The onClick attribute is Javascript anyway, so it is wrong to also include the javascript: pseudo-protocol.
That presupposes that javascript is the default script language. This seem to the case currently in most major browsers but could change in the future.
That presupposes that javascript is the default script language. This seem to the case currently in most major browsers but could change in the future.
Could be but I think it's safer to assume, for the moment, that more likely a new version of ecmascript will become the standard with enhanced features.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
XIII
All-Star
182674 Points
23445 Posts
ASPInsiders
Moderator
MVP
Open a popup window after a button click
May 08, 2005 09:26 AM|LINK
This question seems to be asked more than once so I decided to dedicate a post to it:
<%@ Page Language="C#" %>
<%@ Import namespace="System.Text" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
// Do some other processing...
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("window.open('http://msdn.microsoft.com', '', '');");
sb.Append("</scri");
sb.Append("pt>");
Page.RegisterStartupScript("test", sb.ToString());
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Go!"></asp:Button>
</form>
</body>
</html>
Notice that I imported the System.Text namespace in the @ Import directive. This is because I use the StringBuilder class.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
chinaBoy0115
Member
5 Points
1 Post
Re: Open a popup window after a button click
May 09, 2005 08:52 AM|LINK
This is no a good method , you only want to Open a popup window on client ,why postback to server ?
see :
<%@ Page Language="C#" %>
<%@ Import namespace="System.Text" %>
<script runat="server">
void Page_load(object sender, EventArgs e)
{
Button1.Attributes.Add( "onclick", "popWin()" );
}
</script>
<html>
<head>
<script>
function popWin(){
window.open('http://msdn.microsoft.com', '', '');");
}
</script>
</head>
<body>
<form runat="server">
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Go!"></asp:Button>
</form>
</body>
</html>
SomeNewKid
All-Star
45894 Points
8027 Posts
Re: Open a popup window after a button click
May 09, 2005 09:16 AM|LINK
You need to update your code as follows:
void Page_load(object sender, EventArgs e)
{
Button1.Attributes.Add( "onclick", "popWin();return false;" );
}
Without that additional Javascript statement, the button would still cause a postback.
XIII
All-Star
182674 Points
23445 Posts
ASPInsiders
Moderator
MVP
Re: Open a popup window after a button click
May 09, 2005 11:00 AM|LINK
Maybe I should've been more specific. The original poster of the question actually asked for server side processing (putting values in Session) and instead of doing a response.redirect, open up a new window. So in this case it makes sense to do it like I did in my post.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Rick Scott
Member
75 Points
15 Posts
Re: Open a popup window after a button click
May 25, 2005 09:11 PM|LINK
Button1.Attributes.Add("onclick", "javascript:window.open('www.microsoft.com'); return false;");
That way the code is all in one place (in the code behind file) instead of having part of it in each file. Removing the "return false;" part will allow the button to continue with its postback.
Expanding on this, you can even do fun things like this:
Button1.Attributes.Add("onclick", "javascript: return window.confirm('Are you sure you REALLY want to click that button?');");
SomeNewKid
All-Star
45894 Points
8027 Posts
Re: Open a popup window after a button click
May 25, 2005 10:33 PM|LINK
The onClick attribute is Javascript anyway, so it is wrong to also include the javascript: pseudo-protocol.
That is, the following is wrong:
Button1.Attributes.Add("onclick", "javascript:window.open('www.microsoft.com'); return false;");
And the following is correct:
Button1.Attributes.Add("onclick", "window.open('www.microsoft.com'); return false;");
We use the javascript: pseudo-protocol only when altering an href attribute, as by default that is an http:// protocol.
That is, the following is wrong:
<a href="window.open('www.microsoft.com');">Visit Microsoft</a>
And the following is correct:
<a href="javascript:window.open('www.microsoft.com');">Visit Microsoft</a>
I agree with everything else you've said, however.
saurabhj
Member
35 Points
10 Posts
Re: Open a popup window after a button click
May 10, 2006 01:11 PM|LINK
Is it somehow possible to do this without doing a PostBack (RegisterStartUpScript) requires a postback to work.
Why I am asking this is because I want to run a javascript function after doing some server side processing.
Thanks.
- Saurabh
Its only the impossible that takes some time.
Website: http://www.activeciti.com
Blog: http://blog.saurabhj.com
XIII
All-Star
182674 Points
23445 Posts
ASPInsiders
Moderator
MVP
Re: Open a popup window after a button click
May 10, 2006 03:48 PM|LINK
Hi,
if you're doing some server side processing first that also does need a postback or it should be that you're doing it in the page_load so the javascript gets loaded the first time you make a request to the webform.
The original code was indeed adding the javascript after a button's Click event was handled, hence the postback.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Caffeine
Member
25 Points
5 Posts
Re: Open a popup window after a button click
May 31, 2006 11:27 PM|LINK
>The onClick attribute is Javascript anyway, so it is wrong to also include the javascript: pseudo-protocol.
That presupposes that javascript is the default script language. This seem to the case currently in most major browsers but could change in the future.
XIII
All-Star
182674 Points
23445 Posts
ASPInsiders
Moderator
MVP
Re: Open a popup window after a button click
Jun 01, 2006 05:07 AM|LINK
Could be but I think it's safer to assume, for the moment, that more likely a new version of ecmascript will become the standard with enhanced features.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!