Returning Values from Popup window using ASP.NET

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 Button
Dim i As Integer
btnLookup = e.Item.FindControl('btnLookup')
If Not IsNothing(btnLookup) Then
btnLookup.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 HyperLink
Dim i As Integer
hypSel = e.Item.FindControl("lnkSelect")
If Not IsNothing(hypSel) Then
hypSel.NavigateUrl = "javascript:SetValues('" & e.Item.DataItem("SKU") & "','" & e.Item.DataItem("Price") & "','" & e.Item.DataItem("Description") & "')"
End If

if Button or any other thing else then
Button.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 DataRow
Dim strValue As String

strValue = hidValue.Value

Dim arrChar(0) As Char
Dim arrValues() As String

dtSKU = CType(ViewState.Item("SKU"), DataTable)

arrChar(0) = "/"
arrValues = strValue.Split(arrChar)

If arrValues.Length <> 0 Then
drSKU = dtSKU.NewRow
drSKU(0) = Request.QueryString("CustomerNo")

drSKU(0) = arrValues(0)
'''' get all other values
End 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

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish