If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Open a New Window and Pass Parameters in ASP.Net

I am working on an ASP.Net application using VB.

My main window contains a datagrid which allows multiple records to be selected. When I click on a print button I want to open a new window passing a string compiled from the records that have been selected in the datagrid. In the new window, I am using a third party software called PDFPrint to print the selected items. The one thing that I have run into with PDFPrint is that the code to trigger it seems to only work properly if coded in the html section. I looked at adding it to the code behind, but there doesn't appear to be any way of adding PDFPrint as a reference.

When I select the print button from the main window I want to do three things.
The first is gather all of the records selected.
Next I want to reset the selected records in the datagrid so that none are selected.
The third thing I want to do is open a new window which will use the parameter string to print the selected records.

When the printing is completed and I close the window, I would like to return to the main window at the same location in the datagrid that I was at when I called the new window.

The dataset on your grid should have a column which is the key column in the data displayed
When clicked on the print event ... gather all these key values and form a delimited query string
Use window.open and initiate your pop up

On your pop up code behind Read Context.request.QueryString[] and get your delimited string of key values

Now convert this string to a array of key values/ string array

Get all your records from the dataset on this page and then use container.dataItem on the client side since you say PDFPrint needs it on the client side

The reason I am not passing records between pages is because, of the size and bulk of it I could have used Session but I am against bulky objects like records being passed through sessions

Also when i use a query string I can access the query string on the client and server of the popup precisely why i did this.. If you do not want to do any work on the pop up server side and you have a limited number of records to be printed only .. then you could consider passing the records as delimited query strings and using javascript function to break the query string back to its record format.. there are a lot of javascript code snippets available to do create and break down the query strings

You are right about the key column. I am using the key value to create a the querystring which I will need to pass to the popup. One thing that has been confusing me is initiating the window.open after creating the querystring. I was trying to compile the querystring in the code behind, but I haven't found a way to make "window.open" work in the code behind. Is there any way that I can perform the window.open in the code behind or should I be creating the querystring within the html code?

I agree with you completely about not passing the total records selected to the popup window. Also, thanks for the additional comment on resetting the selected index.

Here is my current process in the code behind of the btnPrint onClick Event:
1. I loop through the datagrid records and see if a record is selected.
2. If selected, I add the key value to my selection list and then reset the value.
3. I was currently using a Session variable to pass my value.
4. This is the point where I am stuck. Doing a response.redirect or server.transfer will clear out all of my information about my current page and I have been unsure about how to get the window.open to work in the code behind.
I have also been unsure of how to get the main window to refresh upon close of the popup.

On the main window when you are completed with your process of gathering data and creating the querystring , add it to session then
add this script
StringBuilder st = new StringBuilder();
st.Append("<script language="javascript>");
st.Append("var w = window.open('url','PopUpWindowName','window attributes');");//opens the pop up
st.Append("w.focus()");//sets the focus on to the pop up
st.Append("</script>");
Page.RegisterStartUpScript("PopUpwindowOpen",st.ToString());[
This script will be added before the page is rendered just before the closing </form> tag

On the pop up complete everything you want and then add this script
StringBuilder st = new StringBuilder();
st.Append("<script language="javascript>");
st.Append("window.opener.location.reload();");// refreshes main window (normally postback)
st.Append("self.close();");//closes the pop up
st.Append("</script>");
Page.RegisterStartUpScript("PopUpClose",st.ToString());

Couple of things to remember
If the parent window opens the pop Up after the it has done postbacks, the reload will cause a postback and not a page load, if you want a page.Load u will have to use window.open to open the parent as below("url","<windownameusedtoopenparent>","windowattributesasbefore");
This should help u

srinivas_s, your code is great, althoug I have an issue with pagereload on postback. Once I close child window and parent window refrshes, it automaticaly relaunches that child window. Would you have a suggestion on how to stop that. Your input would be much appreciated.

Its quite surprising that this is opening up the child window. I use this kind of code all the time to deal with dialogues

It would be great help if you mention where u have placed the open window script and the close window in ur code behind, if possible post a sample
Which event handlers have you posted these pieces of code?.

I am working on something similiar to this and was wondering if you could help me out. I am using ASP.NET C#, and relatively new to it, to open a new window that will contain simple information such as the current sites title for example. The problem I have is that I do not know how edit the page I open. I try opening a blank web page with this code

Thanks for the reply. I tried doing as suggested, the problem I have is what to declare newWindow as. Since I am doing the code in Visual Studio and using C#, it is not recognizing the key word var. I am using Visual Studio 1.1 and not sure as to which version of C#. Is there any other way to declare the variable?

I managed to work this out by using Cookies.
I have a button "Print" on the form which executes javascript code "window.open(PrintForm.aspx)". So, when you click the button, a new window(or tab) is opened which shows PrintForm. In the PrintForm I get data needed for printing from Cookies.

This code is executed in Form load event. PrintForm also has javascript code for automatic printing window.print(); So whenever PrintForm is shown, it gets data from Cookie, shows form and starts printing.

All I needed to ensure is that I write proper data in the Cookie object before Print button is clicked.

Everyone has given nice suggestions and solution to your problem . But I would like to tell you to upgrade with technology. I mean why are you still using VB rather than C#. Same as why don't you start using rad controls and silverlight controls. It will make your efforts appealing dear.