0

I may be going about this all wrong but I am trying to set up a normal HTML button on the client side, using javascript and Window.Open to determine the page url, size, position, etc.

What I would like to do is pass local variables from code-behind and some how attach the query string segment to the url within the Window.Open script dynamically. What I was thinking of doing is constructing the query string in the code behind and assign it to a string variable. I would the like to simply attached the variable from code-behind to the query string segment of the url, in the Window.Open script.

Something like this, although I this isn't working...

<input type="button" value="Printer Friendly Copy" onclick="window.open('Print.aspx?<%= MyString %>', '_blank', 'toolbar=no, scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400')" /> 

Could anyone tell me the proper way of doing this? I could navigate to a new page all together using Response.Redirect in code-behind but I would prefer to open a new window and so far, javascript seems to be the best way of doing this from my research.

2 Answers 2

3

It's not entirely clear to me what you're asking, but if you simply want to open a new window with a url that contains parameters, that is certainly possible.

Note that you must open the new window only in response to a UI event, like a button click, or the browser will assume it's malicious and block it.

My question is what you are doing with

<%= MyString %>

is that a variable you want to fill in? If so, you should simply construct the url in Javascript.

var url = "Print.aspx?" + variables;
window.open(url, ...)

Here is a simple fiddle showing this: http://jsfiddle.net/LvB7Z/1/

Sign up to request clarification or add additional context in comments.

1 Comment

That makes perfect sense. What I'm trying to to though is construct the parameters dynamically according to certain conditions in code-behind. I guess I'm trying to associate a C# variable with JavaScript, filling in the parameters with the C# variable values. I'm a bit flaky with it comes to JS though.
2

Set your value in the code-behind into a hidden field with a unique name or class. On the client side, use javascript/jquery to find that hidden field and then use window.open passing it the query string key/value.

For example, var vals = $('#myHiddenField').val(); in jquery notation. or use getElementById in js.

HTH

3 Comments

I suppose I could create a hidden label on the page_load even with a per-determined parameters and pass the label text value with JS.
This got it. I'm able to first assign my parameter data to a hidden label and the read the label text with my JS function. <script type="text/javascript"> function NewPrintWindow() { var QueryParameters = $('<%= hlReceiptData %>').val() Window.Open("Print.aspx?" + QueryParameters, "_blank", "toolbar=no, scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400")} </script>
In code behind, I assign the hidden label's text as.. ` string ReceiptData = "PatientFirstName=" + PatientFirstNameLabel.Text + "&" + "PatientLastName=" + PatientLastNameLabel.Text + "&"; hlReceiptData.Text = ReceiptData.ToString(); `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.