Returning Values from Popup window using ASP.NET

A few days back i got a problem that i want to get values from pop up using javascript from ASP.net application. from the help of and guidance of my colleages i figure out a way of doing it . here are

ITPro Today

July 4, 2005

3 Min Read
ITPro Today logo in a gray background | ITPro Today

A few days ago, I ran into a problem in which I wanted to get values  from a pop up usingJavaScript from an ASP.NET applications. With the help and guidance of my colleagues, here are some steps to help you.

First you'll need to create a parent page and then create a pop up page.

1. Add a hidden HTML text box control to page where you need the values (Parent). Set it to runat server (hidValue)

2. Add a HTML Button control to the page where you need values (Parent). set it to runat server (btndummy)

3. Add the JavaScript to open the popup window set it size and attributes (Parent).

function SKUWindow(){window.open('SKULookup.aspx',null,'height=300,width=362,status=yes,toolbar=no,menubar=no,location=no');}


4. Add JavaScript caller function in to the button from you need to generate popup in my case there was a grid where i need to have button on every row here is the code 'Check if it is an item or alterning item then run the code

below (Parent)Dim btnLookup As ButtonDim i As IntegerbtnLookup = e.Item.FindControl('btnLookup')If Not IsNothing(btnLookup) ThenbtnLookup.Attributes.Add('OnClick','javascript:return SKUWindow();')End If'if Button or any other thing else then'Button.Attributes.add('OnClick', 'javascript:return 'SKUWindow();')


5. In the Popup window form add the javascript to get and return the values to the parent form. here is the sample.

function SetValues(SKU, Description, Price){var Value = SKU + "/" + Description + "/" + Price;window.opener.document.forms[0].hidValue.value=Value;window.opener.document.forms[0].btndummy.click();window.close();return false;}


Add this function to any place where you need to call the actions to of send values in my case there was hyperlink with Select. (Child)

Dim hypSel As HyperLinkDim i As IntegerhypSel = e.Item.FindControl("lnkSelect")If Not IsNothing(hypSel) ThenhypSel.NavigateUrl = "javascript:SetValues('" & e.Item.DataItem("SKU") & "','" & e.Item.DataItem("Price") & "','" & e.Item.DataItem("Description") & "')"End Ifif Button or any other thing else thenButton.Attributes.add("OnClick", "javascript:SetValues("Value1","Value2","Value3")")


The above function receives all 3 values as parameters which i need to send to parent page. Concatinate the values into one string and set these values to parent forms hiddenTextbox control created in step 1 and call the onClick event of HTML button control created in step 2. Close the current window and return false to end call.

When you will click the Select button current window will be closed and the parent form will call the OnClick Event of HTML button control created in step 2 and values of the pop up will be written to the HTML text box. Add code the event in server side to get the values from HTMl Text box and do what ever you want with them in my case i create a row of item and add them to a a database and show on the grid. so here is the code

btnDummy_OnClick(e as eventargs, obj as object)Dim drSKU As DataRowDim strValue As StringstrValue = hidValue.ValueDim arrChar(0) As CharDim arrValues() As StringdtSKU = CType(ViewState.Item("SKU"), DataTable)arrChar(0) = "/"arrValues = strValue.Split(arrChar)If arrValues.Length <> 0 ThendrSKU = dtSKU.NewRowdrSKU(0) = Request.QueryString("CustomerNo")drSKU(0) = arrValues(0)'''' get all other valuesEnd If

It is very easy way to get values from the pop . I would like to thank Ihsan and danish for the guidance.

Thanks and Regards

Yasir Attiq Butt

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like