Search code examples
javascriptasp.netwindow.opener

ASP.NET: Programmatically fire a server-side event in window.opener with JavaScript


I have a DropDownList that fires off some server-side databinding in its OnSelectedIndexChanged event.

<asp:DropDownList ID="ddlGroup" runat="server" 
     AutoPostBack="True" OnSelectedIndexChanged="SelectGroup" />

Elsewhere in the page, some JavaScript opens a popup. When the popup is filled out and submitted, I want to use JavaScript to fire that OnSelectedIndexChanged event in the opener page. I found some other code that does something similar:

    if (window.opener != null ) {
    var cf = window.opener.document.forms['aspnetForm'];
        if (!cf) {
            cf =  window.opener.document.aspnetForm;
        }
        cf.__EVENTTARGET.value = "prAdded";
        cf.__EVENTARGUMENT.value = "winClosed";
        cf.submit(); 
    }

I think this is what I'm looking for, but I'm not sure what should go in the EVENTTARGET and EVENTARGUMENT parts, or even if I need those at all. I want to specifically fire the OnSelectedIndexChanged event handler for ddlGroup. Is this possible/practical?

Secondary question: Can I make the parent page refresh AFTER I run server-side code in the popup?


Solution

  • Eh, you could do it that way, but I'd just use __doPostback() instead. That sets __EVENTTARGET and __EVENTARGUMENT to the two parameters, and assuming your first parameter is the UniqueID of an UpdatePanel, causes just that UpdatePanel to refresh.

    So either you can set things up so refreshing the updatepanel does what you want to happen, or you can check those values on postback -- Request.Form["__EVENTTARGET"] ... and go from there.