views:

69

answers:

2

A situation requires me to order javascript code on a page be executed with an argument calculated in codebehind in ASP.NET

Here is the situation:

I have a page called search.aspx . This page contains a button and a textbox. Users put their arguments for search in the textbox and then click the button. The button posts back and runs a button click method. This code behind logic (running on the server) serializes and inserts the contents of the textbox to a DB.

Assuming a rowID or something to identify the serialized query by will be returned inside the button click method, how can I then tell the page (search.aspx) to open a new tab with results.aspx?query=.

I know I have to use javascript as the code behind can't open a new tab, but I am just wondering how to do so.

I've never used JS so a maximum amount of details in the answer is better.

+1  A: 

To directly answer your question, you can insert something like the following directly in the page output just before the body end tag.

 <script type="text/javascript">
        window.open("http://servername/pagename.aspx?queryid=blah");
 </script>

by using Response.Write.

However, did you also consider simply opening a new window on the button click with the button's text passed in as a query string to an aspx page?

I did, :-) can't have that for security reasons though.
Matt
+2  A: 

Assuming your rowID is loaded in a variable named, well, rowID, then put this at the end of your click event (VB.Net):

ClientScript.RegisterStartupScript(me.GetType(), "results", _
    string.Format("window.open('results.aspx?query={0}','Search Results','status=1')", rowID), True )
Joel Coehoorn