Popup and .Net

 

Did you ever played with popup windows with .Net ?

Well I did and this is my not so successful experience.

OK, here is the scenario:

- One master page with different controls. The one I am going to talk about is a Poll control, you know where you ask the user an opinion on something and when he click on Vote, you show the results for every question.
- Due to design requirements, the result page must be render in a popup window.

The Poll results window is not really the issue here, it's just reading the data and create few bar charts to show the pecentage of votes.
To open a popup window from a click on the vote button, I attached an attribute from my code, to open the window.
The idea is that I need also to save the vote by incrementing the right field in the database.

What I discovered very quickly, is that the window open before the event is fired.

So indeed the vote is not save and the results show the wrong value.

This is what solutions I tried before the last one which I reckon is far to be perfect, but it works.

  1. View state is of course not an option here because I go to a separate window, so I tried to pass to a querystring the value the user click.
    No success, the window opened and the querystring was not there.

  2. Using Session object, I was thinking that this will let me keeping the value, having the window open, read the Session object, and record the Vote from the popup before displaying the Results.
    No because .Net is probably too fast ! No seriously what's seem to happen is that the Session object is created from the main page after the popup open. At this stage I was really startting to give up.

  3. Final assault on the code, and what I did is quite simple. I add to my datatable a new field, kind of flage.
    I store the Vote from the Main page, and of course the client side script open the window before the SaveThisVote subroutine start.
    But when I save the vote I set the boolean flag to True, and in my popup code, I execute a loop with Response.redirect until I have a flag set to True.
    It works fine on my local machine, but I don't think its' the right thing to do. I am worrying that the wp process freeze, because what I am doing there is indeed a very unmanaged thread.

The other option I am working on is to see if I can modify programmatically the attribute I link to the Vote button.
Doing this I hope to be able to include the querystring I need for the vote.
I should probably doing that from a simple client side script link to each radio buttons.


6 Comments

  • You can call functions in the opener window like this:





    window.opener.myFunction();





    So, the usual approach I take is to have my popup dialog take a parameter called "Handler" which is the name of a javascript function in the main page. Something like this in the caller page:





    function myHandler(controlID, value)


    {


    // do something, such as


    // calling "__doPostBack" to fire


    // an event handler.


    }





    And in the popup dialog, you do something like this:





    void MySubmitButton_Click(object sender, EventArgs e)


    {


    Page.RegisterStartupScript("__close", String.Format("<script>window.opener.{0}({1},{2})</script>", Request["Handler"], Request["ControlID"], someValue));


    }

  • Thanks Jesse, but I understood well, this is exactly the opposite effect I want to achieve.





    I click on a button on the main page and I want to open a window.





    I want to keep the vote selected store it in the database and show in the popup the poll results.


    so I could save the vote from the Main page or from the popup, but I want to be sure to include the last vote in the results display on the poll.


    Your example seem to do the opposite, it fire an event from the popup.


  • No, that is exactly what the example is. I assumed you already knew the easy part, popping up the window. The example shows the solution to your problem, how to communicate back with the parent page after your results are submitted.

  • Jesse can you be more explicit? Because your code tell me that you fire an event from the popup to the caller page AFTER closing the popup page.


    If it's the case, I don't want to do that ;-)


    What I want is clearly record the vote, open the popup and show the results INCLUDING the last vote.


    What's happen if the user let the window open ? Nothing is return to the caller page.


    Sorry If I misunderstand what you said !

  • a better handler in the popup would look like this:





    void MySubmitButton_Click(object sender, EventArgs e)


    {


    // Do Some Processing





    Page.RegisterStartupScript("__close", String.Format("<script>window.opener.{0}({1},{2}); window.close();</script>", Request["Handler"], Request["ControlID"], someValue));


    }





    So, what happens is a user clicks a button in your popup (for example, a "Save" button). You do some processing on the data, like saving it to a database. After you have done this, you send a javascript to the browser that fires calls a function on the parent window, and then closes the popup. The function on the parent window is responsible for making sure that the page refreshes. There are a million ways to do this. You could just use DOM with DHTML to update the page automatically, without a refresh. Or, you could use the __doPostBack function to initiate a postback event. Or, you could do something like "window.location = window.location". To simulate a page refresh. Personally, I like using DHTML to dynamically update the page best, but some times that is not an option and firing a custom __doPostBack event is best for those cases.


  • Sorry to be stubborn Jesse but the user click Save on the main page not on the popup. the popup has no button. It's only there to display the results of the different votes.


    Of course I can have a close this window button, but if the user don't do anything on this window, what's happen.


    anyway I like the suggestion and I think I could be able to include this code on a on load event in Javascript.

Comments have been disabled for this content.