Open a New Window and Pass Parameters in ASP.Net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Open a New Window and Pass Parameters in ASP.Net

  1. #1
    Join Date
    Nov 2004
    Posts
    17

    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.

    Does anyone have any tips or suggestions as how to do this?

    Thanks for your help,
    Carey

  2. #2
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Try this...

    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

    I amnot sure of it answers your question , hope this helps

  3. #3
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Just after you created the querystring after gathering key vlues of the records selected reset the selectedindex of the grid.. forgot this part to be added

  4. #4
    Join Date
    Nov 2004
    Posts
    17
    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.

    Thanks for the help you have provide me.
    Carey

  5. #5
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    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

    Thanks

  6. #6
    Join Date
    Nov 2004
    Posts
    17
    Thanks srinivas_s. Everything works great. I appreciate you taking the time to help me out. It has definitely saved me a lot of headaches.

    Thanks Again

  7. #7
    Join Date
    Jun 2005
    Posts
    1
    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.

  8. #8
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Hi

    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?.

    Are you using IFrames in any case?

  9. #9
    Join Date
    Jun 2007
    Posts
    4
    Hi all,

    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

    public void newWindow_click(object sender, EventArgs e)
    {
    string popupScript = "<script language='javascript'>" +
    "window.open('', 'Google', " +
    "'width=1000, height=1000, menubar=yes, resizable=yes')" + "</script>";

    Page.RegisterStartupScript("Google", popupScript);
    }

    This launches a new blank page but I am having trouble trying to edit the blank page to even just write sentences on it, any help would be greatly appreciated.

    Thanks

  10. #10
    Join Date
    Jun 2007
    Posts
    1
    You can do something like:

    string popupScript = "<script language='javascript'>" +
    "newWindow=window.open('', 'Google', " +
    "'width=1000, height=1000, menubar=yes, resizable=yes')" + "</script>";

    and then use the reference to newWindow to access the newly created window. You need to declare the newWindow var somewhere in the calling page so it has page level scope, so you can use it later.

  11. #11
    Join Date
    Jun 2007
    Posts
    4
    Yoness,

    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?

    Thanks

  12. #12
    Join Date
    Dec 2009
    Posts
    1
    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.

    HttpCookie Cookie = Request.Cookies["PrintData"];
    TextBox1.Text = Cookie["Data"];

    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.

    HttpCookie C = new HttpCookie("PrintData");
    C["Data"] = TextBox1.Text;
    Response.Cookies.Add(C);

  13. #13
    Join Date
    Sep 2010
    Posts
    11
    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.

  14. #14
    Join Date
    May 2011
    Posts
    1
    I want the code for open the user's profile or page and show his own info after log in! please help me.!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center