[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how to call javascript function from codebehind to open a new window

Posted on 2009-12-24
7
Medium Priority
?
750 Views
Last Modified: 2012-05-08

how to call javascript function from codebehind to open a new window but it should not be pop up window because most of the users keep pop up blocker on


i have used one the code snippet given by your site only but it does not work pls help


In your specific case, to call the javascript function that Daryal typed out, you would want a C# codebehind method that looks specifically like this:

protected void OpenWindow(string name, int age)
{
      StringBuilder sbJavaScript = new StringBuilder();
      sbJavaScript.AppendLine("<script type=\"javascript\">");
      sbJavaScript.AppendLine("OpenNewWindow(\"" + name + "\", " + age.ToString() + ");");
      sbJavaScript.AppendLine("</script>");
      ClientScript.RegisterStartupScript(Page.GetType(), string.Empty, sbJavaScript.ToString());
}

Just make sure that you have the below JavaScript function within your html markup:

<script type="text/javascript">
function OpenNewWindow(name,age)
{
window.open('http://xxx.com?name="+name+"&age="+age+")
}
</script>


pls help

thanks
0
Comment
Question by:vedant0500
7 Comments
 
LVL 8

Expert Comment

by:jtdebeer
ID: 26118910
Can you use this:

<asp:HyperLink ID="hyp" NavigateUrl="~/aboutus.aspx" runat="server" Text="click" Target="_blank"></asp:HyperLink>


Or set the NavigateURL in your code behind.

Have fun!
0
 

Author Comment

by:vedant0500
ID: 26119002
dear jtdebeer

no i want to use this



thanks
any other solution
0
 
LVL 21

Expert Comment

by:silemone
ID: 26119303
ok...the problem is what even do you want to associate the click with?

an event must call
protected void OpenWindow(string name, int age)


is that happening in your code?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 21

Expert Comment

by:silemone
ID: 26119319
just a question, but why can't you build the script in the .aspx page and then call it and pass the appropriate params?   I'm still confused as to why this javascript has to be created in the code behind...
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 26120030
can you try to modify you code like below:
    sbJavaScript.AppendLine("window.open('http://xxx.com?name="+name+"&age="+age.ToString()+"')");

Point is to just inject the code and call window.open method.
Check this link:
http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx
(Injecting Script blocks section)
0
 
LVL 14

Accepted Solution

by:
amar31282 earned 1500 total points
ID: 26122326
Go for it..

Response.Write("window.open('PageName.aspx','','')");

This will open new window.. you can make the other setting for the popup in window.open settings ..

Cheers..
0
 

Author Closing Comment

by:vedant0500
ID: 31669759
Solution was partially helpful
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question