how do i send data from a popup window back to the calling page in ASP.NET 2.0? (VB)

I have a main page that needs a textbox filled with a product ID, since there are so many products, I decided to create a popup window with a treeview that allows a user to select a product  from a selected category and click on the product name (a link or button or anything) that would post the productId back to the main page.
I've seen examples of doing this with session variables but that seems a bit quirky.
Is there any simple way of posting data back to a calling page from a popup window?
Thanks!
bd9000Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bane83Connect With a Mentor Commented:
Very easy with javascript.  Add this to a the link/button/whatever's client event that fires when you make the selection:

window.opener.document.getElementById('TextboxToHoldProductID.ClientID').value = [this.value];

You'll likely have to hard code the TextboxToHoldProductID.ClientID string as there's no way that I can think of right now to get the ClientID from window to the other (unless you pass it in using a querystring, so you might want to consider this instead of hardcoding --> window.open('MyPopup.aspx?ControlID=" + this.id, ....)).

Also [this.value] is to be replaced with whatever needs to be filled in the textbox.  This is dependent on the control you use.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>I've seen examples of doing this with session variables but that seems a bit quirky.
it's the best way, and I don't see anything quirky about that?
you might want to check out to find javascript examples instead, but those will eventually fail to be cross-browser compatible...
0
 
bd9000Author Commented:
I've seen in the examples about registering popup pages - do I need to do this with ASP.Net 2.0 even though I'm not actually doing a postback (w/JavaScript)?

- on another note, I've found that as long as you use GetElementByID, most browsers (IE, FF, Opera, NS & Safari) will work fine. I think JavaScript is pretty much the defacto standard now - anyone using an ancient browser probably can't afford our products anyway :)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Bane83Commented:
Not sure what you mean about registering, but I've done this exact thing before and I didn't have to do anything other than a window.open() in my main window and window.opener.document.getElementById()... in the popup.
0
 
bd9000Author Commented:
Ok, I got it to work. I discovered that the button control on the main page causes a reload of the main page when clicked even if the UseSubmitBehavior=false and CausesValidation=false (by design, I presume), so I added an HTML button to the form and put the popup opening function in it's OnClick event (not documented by VS2005) to open the popup window without a page refresh/reload.

Here's the final working code (test page) with 2 different buttons to show that the second one does not cause a page refresh of the calling page.
<!-- Main Calling Form -->
<form id="frmMain" runat="server">
<div>
<asp:Button ID="btnOpenPopUp" runat="server" CausesValidation="False" EnableViewState="False" OnClientClick="window.open('popup2.aspx');" Text="Open PopUp" UseSubmitBehavior="False" />
<br />
<input id="btnOpenPopUp2" type="button" value="Open PopUp 2" OnClick="javascript:window.open('popup2.aspx');" />
<br />
<asp:TextBox ID="txtReturnValue" runat="server"></asp:TextBox>
</div>
</form>
 
<!-- PopUp Form -->
<form id="frmPopUp" runat="server">
<div>
<asp:Button ID="btnReturnValue" runat="server" Text="Return Value And Close" CausesValidation="False" EnableViewState="False" OnClientClick="window.opener.document.getElementById('txtReturnValue').value = [window.document.getElementById('txtValueToReturn').value];window.close();" UseSubmitBehavior="False" />
<br />
Enter value to return to main form
<br />
<asp:TextBox ID="txtValueToReturn" runat="server">123</asp:TextBox>
</div>
</form>

Open in new window

0
 
Bane83Commented:
The postback is because asp:Buttons render as submit buttons.

In order to disable the postback, you would need to add OnClientClick="return false;" to the button.  This cancels the postback event.
0
 
bd9000Author Commented:
Sounds good! Thanks again.
0
All Courses

From novice to tech pro — start learning today.